+ struct put_state *ps = (struct put_state *) *ptr;
+ char *username;
+ char password[33];
+
+ if (*ptr == NULL) {
+ if (strcmp(method, MHD_HTTP_METHOD_POST) != 0)
+ return respond_405(connection);
+
+ if (url[4] == '/')
+ return respond_404(connection);
+
+ *ptr = (void *) 1;
+
+ username = MHD_digest_auth_get_username(connection);
+ if (username == NULL)
+ return respond_401(connection, MHD_NO);
+ auth_get_password(username, password);
+
+ ret = MHD_digest_auth_check(connection, REALM, username, password, 300);
+ free(username);
+
+ if (ret == MHD_INVALID_NONCE || ret == MHD_NO)
+ return respond_401(connection, (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO);
+
+ struct put_state *ps = malloc(sizeof(struct put_state));
+ ps->data = NULL;
+ ps->pp = MHD_create_post_processor(connection, 4096, &POST_put_iterator, ps);
+ *ptr = ps;
+ return MHD_YES;
+ }
+
+ if (*upload_data_size) {
+ MHD_post_process(ps->pp, upload_data, *upload_data_size);
+ *upload_data_size = 0;
+ return MHD_YES;
+ }
+
+ if (ps->data == NULL || ps->data_size == 0)
+ return respond_JSON_Failure(connection);
+
+ username = MHD_digest_auth_get_username(connection);
+ struct blerg *b = blerg_open(username);
+ if (b == NULL)
+ return respond_JSON_Failure(connection);
+ if (blerg_store(b, ps->data, ps->data_size) == -1) {
+ blerg_close(b);
+ return respond_JSON_Failure(connection);
+ }
+ blerg_close(b);
+
+ MHD_destroy_post_processor(ps->pp);
+ free(username);
+ free(ps->data);
+ free(ps);
+ *ptr = NULL;
+
+ return respond_JSON_Success(connection);
+ } 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_405(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;
+ }
+
+ 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);
+ *ptr = NULL;
+
+ return respond_JSON_Success(connection);