Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Basic operations work.

Ofcourse, it doesn't run unless you babysit it with Valgrind
  • Loading branch information...
commit fab4d6a2c84c4f16b876fe8507d5db8606b8921b 1 parent 8c1225e
@yuvipanda authored
Showing with 26 additions and 13 deletions.
  1. +26 −13 server.c
View
39 server.c
@@ -8,8 +8,6 @@
#include <microhttpd.h>
#include <tdb.h>
-#include <sys/filesys.h>
-
#define PORT 8888
@@ -28,26 +26,41 @@ int answer_to_connection (void *cls, struct MHD_Connection *connection, const ch
printf("\nSetup nginx properly!");
return MHD_NO;
}
-
+
+ TDB_DATA key, data;
+ key.dptr = strdup(url);
+ key.dsize = strlen(url);
+
+ response = MHD_create_response_from_data (strlen(page),
+ (void*) page, MHD_YES, MHD_NO);
+
if (strcmp(method, "GET")==0)
{
-
+ TDB_DATA data;
+ data = tdb_fetch (db, key);
+ if (data.dptr)
+ {
+ MHD_add_response_header (response, "Location", data.dptr);
+ ret = MHD_queue_response (connection, MHD_HTTP_MOVED_PERMANENTLY, response);
+ }
+ else
+ {
+ ret = MHD_queue_response (connection, 404, response);
+ }
}
else if (strcmp(method, "POST")==0)
{
printf ("%s -> %s", url, upload_data);
- TDB_DATA key, data;
- key.dptr = url;
- key.dsize = strlen(url);
- data.dptr = upload_data;
+ data.dptr = strdup(upload_data);
data.dsize = strlen(upload_data);
tdb_store(db, key, data, TDB_REPLACE);
-
+ ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
}
- response = MHD_create_response_from_data (strlen(page),
- (void*) page, MHD_NO, MHD_NO);
- ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
+
+ free(data.dptr);
+ free(key.dptr);
+
MHD_destroy_response(response);
return ret;
@@ -58,7 +71,7 @@ int main (int argc, char** argv)
struct MHD_Daemon *daemon;
TDB_CONTEXT *db;
- db = tdb_open ("./linksdb", 0, NULL, O_CREAT | O_RDWR, 0666);
+ db = tdb_open ("linksdb", 0, NULL, O_CREAT | O_RDWR, 0666);
daemon = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY, PORT, NULL, NULL,
&answer_to_connection, db, MHD_OPTION_END);
if (daemon == NULL)
Please sign in to comment.
Something went wrong with that request. Please try again.