#include <microhttpd.h>
#include "database.h"
#include "tags.h"
+#include "auth.h"
+#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 record.</body></html>"
+
+struct create_state {
+ struct MHD_PostProcessor *pp;
+ char username[33];
+ char password[33];
+};
+
+struct MHD_Response *response_404;
+struct MHD_Response *response_501;
+struct MHD_Response *response_JSON_Success;
+struct MHD_Response *response_JSON_Failure;
+
+void init_responses() {
+#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);
+
+#define JSON_FAILURE "{status: \"failure\"}"
+ response_JSON_Failure = MHD_create_response_from_data(strlen(JSON_FAILURE), JSON_FAILURE, MHD_NO, MHD_NO);
+}
+
+int respond_404(struct MHD_Connection *connection) {
+ 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_JSON_Success(struct MHD_Connection *connection) {
+ return MHD_queue_response(connection, MHD_HTTP_OK, response_JSON_Success);
+}
+
+int respond_JSON_Failure(struct MHD_Connection *connection) {
+ return MHD_queue_response(connection, MHD_HTTP_OK, response_JSON_Failure);
+}
+
+int parse_url_info(const char *url, char *author, uint64_t *record) {
+ const char *c;
+ int ret = 0;
+ int len;
+
+ c = strchr(url, '/');
+ if (c == NULL) {
+ len = strlen(url);
+ } else {
+ len = c - url;
+ }
+ if (len == 0)
+ return 0;
+ memcpy(author, url, len);
+ author[len] = 0;
+ ret |= URL_INFO_AUTHOR;
+
+ if (c != NULL && c[1] != 0) {
+ *record = strtoull(c + 1, NULL, 10);
+ ret |= URL_INFO_RECORD;
+ }
+
+ return ret;
+}
+
+int POST_create_iterator(void *cls, enum MHD_ValueKind kind, const char *key, const char *filename, const char *content_type, const char *transfer_encoding, const char *data, uint64_t off, size_t size) {
+ struct create_state *cs = cls;
+
+ if (strncmp(key, "username", 8) == 0) {
+ if (size > 32) size = 32;
+ memcpy(cs->username, data, size);
+ cs->username[size] = 0;
+ } else if (strncmp(key, "password", 8) == 0) {
+ if (size > 32) size = 32;
+ memcpy(cs->password, data, size);
+ cs->password[size] = 0;
+ }
+}
static int
ahc_derp (void *cls, struct MHD_Connection *connection, const char *url, const char *method,
const char *version, const char *upload_data, size_t *upload_data_size, void **ptr) {
struct MHD_Response *response;
int ret, len;
- const char *c;
+ char author[33];
+ uint64_t record;
char *data;
if (strncmp(url, "/get", 4) == 0 && strlen(url) > 4) {
- char author[33];
- uint64_t record;
-
if (strcmp(method, MHD_HTTP_METHOD_GET) != 0)
return MHD_NO;
- c = strchr(url + 5, '/');
- if (c == NULL) {
- len = strlen(url) - 5;
- } else {
- len = c - (url + 5);
- }
- memcpy(author, url + 5, len);
- author[len] = 0;
- printf("author: %s\n", author);
-
- c = url + (5 + len);
- if (c[0] == '/' && c[1] != 0) {
- record = strtoull(c + 1, NULL, 10);
- printf("record: %s %l\n", c + 1, record);
+ if (url[4] != '/')
+ return respond_404(connection);
+ ret = parse_url_info(url + 5, author, &record);
+ if ((ret & URL_INFO_AUTHOR) == 0)
+ return respond_404(connection);
+
+ if (!blerg_exists(author))
+ return respond_404(connection);
+
+ if (ret & URL_INFO_RECORD) {
if (*ptr == NULL) {
*ptr = (void *) 1;
return MHD_YES;
blerg_close(b);
if (ret == 0) {
- response = MHD_create_response_from_data(strlen(NOTFOUND), NOTFOUND, MHD_NO, MHD_NO);
- ret = MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response);
+ return respond_404(connection);
} else {
response = MHD_create_response_from_data(len, data, MHD_YES, MHD_NO);
ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
return ret;
}
}
+ } else if (strncmp(url, "/put", 4) == 0) {
+ char *username;
+ const char *password = "testpass";
+ const char *realm = "Blerg Post";
+
+#define OPAQUE "d29fb6db8f21a6e99903651a9f87470e"
+#define DENIED "DENIED, MOTHERFUCKER"
+#define PAGE "DERP DERP AUTHENTICATED DERP"
+
+ if (*ptr == NULL) {
+ *ptr = (void *) 1;
+
+ username = MHD_digest_auth_get_username(connection);
+ if (username == NULL) {
+ response = MHD_create_response_from_data(strlen (DENIED), DENIED, MHD_NO, MHD_NO);
+ ret = MHD_queue_auth_fail_response(connection, realm, OPAQUE, response, MHD_NO);
+ MHD_destroy_response(response);
+ return ret;
+ }
+
+ printf("username: %s\n", username);
+
+ ret = MHD_digest_auth_check(connection, realm, username, password, 300);
+ free(username);
+
+ if (ret == MHD_INVALID_NONCE || ret == MHD_NO) {
+ response = MHD_create_response_from_data(strlen (DENIED), DENIED, MHD_NO, MHD_NO);
+ ret = MHD_queue_auth_fail_response(connection, realm, OPAQUE, response,
+ (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO);
+ MHD_destroy_response(response);
+ return ret;
+ }
+ }
+
+ *ptr = NULL;
+
+ if (url[4] != '/')
+ return respond_404(connection);
+ ret = parse_url_info(url + 5, author, &record);
+ if ((ret & URL_INFO_AUTHOR) == 0)
+ return respond_404(connection);
+
+ response = MHD_create_response_from_data(strlen(PAGE), PAGE, MHD_NO, MHD_NO);
+ ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
+ MHD_destroy_response(response);
+ return ret;
+ } else if (strncmp(url, "/create", 8) == 0) {
+ struct create_state *cs = (struct create_state *) *ptr;
+
+ if (cs == NULL) {
+ if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
+ return respond_501(connection);
+
+ struct create_state *cs = malloc(sizeof(struct create_state));
+ cs->username[0] = cs->password[0] = 0;
+ cs->pp = MHD_create_post_processor(connection, 1024, &POST_create_iterator, cs);
+ *ptr = cs;
+ return MHD_YES;
+ }
+
+ if (*upload_data_size) {
+ MHD_post_process(cs->pp, upload_data, *upload_data_size);
+ *upload_data_size = 0;
+ return MHD_YES;
+ } else {
+ if (cs->username[0] == 0 || cs->password[0] == 0)
+ return respond_JSON_Failure(connection);
+
+ if (blerg_exists(cs->username))
+ return respond_JSON_Failure(connection);
+
+ struct blerg *b = blerg_open(cs->username);
+ blerg_close(b);
+ auth_set_password(cs->username, cs->password);
+
+ MHD_destroy_post_processor(cs->pp);
+ free(cs);
+ return respond_JSON_Success(connection);
+ }
} else {
- return MHD_NO;
+ return respond_404(connection);
}
}
fd_set rs, ws, es;
int max;
+ init_responses();
+
daemon = MHD_start_daemon(MHD_USE_DEBUG, 8080, NULL, NULL, &ahc_derp, NULL, MHD_OPTION_END);
if (daemon == NULL) {
fprintf(stderr, "Could not start web server\n");