Clean up error responses, add 501 for other than POST to /create
authorChip Black <bytex64@bytex64.net>
Tue, 21 Dec 2010 10:24:48 +0000 (04:24 -0600)
committerChip Black <bytex64@bytex64.net>
Tue, 21 Dec 2010 10:25:21 +0000 (04:25 -0600)
http_blerg.c

index 7a9e497..f8513c5 100644 (file)
@@ -8,8 +8,6 @@
 #define URL_INFO_AUTHOR 0x1
 #define URL_INFO_RECORD 0x2
 #define DERP "DERP DERP DERP"
-#define NOTFOUND "<html><head><title>404 Not Found</title></head><body><h1>404 Not Found</h1>I couldn't find that.</body></html>"
-#define JSON_SUCCESS "{status: \"success\"}"
 
 struct create_state {
        struct MHD_PostProcessor *pp;
@@ -17,20 +15,31 @@ struct create_state {
        char password[33];
 };
 
-struct MHD_Response *response404;
-struct MHD_Response *responseJSONSuccess;
+struct MHD_Response *response_404;
+struct MHD_Response *response_501;
+struct MHD_Response *response_JSON_Success;
 
 void init_responses() {
-       response404 = MHD_create_response_from_data(strlen(NOTFOUND), NOTFOUND, MHD_NO, MHD_NO);
-       responseJSONSuccess = MHD_create_response_from_data(strlen(JSON_SUCCESS), JSON_SUCCESS, MHD_NO, MHD_NO);
+#define CONTENT_404 "<html><head><title>404 Not Found</title></head><body><h1>404 Not Found</h1>I couldn't find that.</body></html>"
+       response_404 = MHD_create_response_from_data(strlen(CONTENT_404), CONTENT_404, MHD_NO, MHD_NO);
+
+#define CONTENT_501 "<html><head><title>501 Not Implemented</title></head><body><h1>501 Not Implemented</h1>I'm sorry, Dave. I'm afraid I can't do that.</body></html>"
+       response_501 = MHD_create_response_from_data(strlen(CONTENT_501), CONTENT_501, MHD_NO, MHD_NO);
+
+#define JSON_SUCCESS "{status: \"success\"}"
+       response_JSON_Success = MHD_create_response_from_data(strlen(JSON_SUCCESS), JSON_SUCCESS, MHD_NO, MHD_NO);
 }
 
 int respond_404(struct MHD_Connection *connection) {
-       return MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response404);
+       return MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response_404);
+}
+
+int respond_501(struct MHD_Connection *connection) {
+       return MHD_queue_response(connection, MHD_HTTP_NOT_IMPLEMENTED, response_501);
 }
 
-int respond_JSONSuccess(struct MHD_Connection *connection) {
-       return MHD_queue_response(connection, MHD_HTTP_OK, responseJSONSuccess);
+int respond_JSON_Success(struct MHD_Connection *connection) {
+       return MHD_queue_response(connection, MHD_HTTP_OK, response_JSON_Success);
 }
 
 int parse_url_info(const char *url, char *author, uint64_t *record) {
@@ -176,7 +185,7 @@ ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const c
 
                if (cs == NULL) {
                        if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
-                               return MHD_NO;
+                               return respond_501(connection);
 
                        struct create_state *cs = malloc(sizeof(struct create_state));
                        cs->username[0] = cs->password[0] = 0;
@@ -201,7 +210,7 @@ ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const c
 
                        MHD_destroy_post_processor(cs->pp);
                        free(cs);
-                       return respond_JSONSuccess(connection);
+                       return respond_JSON_Success(connection);
                }
        } else {
                return respond_404(connection);