]> code.citadel.org Git - citadel.git/blobdiff - webcit-ng/server/upload.c
upload.c: more progress on returning values
[citadel.git] / webcit-ng / server / upload.c
index e2d7d1c30995ad39d185235d925b72e811717211..866be6762b3cc8a6b204965f031594fb513cd2fd 100644 (file)
@@ -7,6 +7,7 @@
 
 #include "webcit.h"
 
+
 // This function is called by the MIME parser to handle data uploaded by the browser.
 void upload_handler(char *name, char *filename, char *partnum, char *disp,
                    void *content, char *cbtype, char *cbcharset,
@@ -33,19 +34,34 @@ void upload_handler(char *name, char *filename, char *partnum, char *disp,
        }
        write(fd, content, length);
        close(fd);
+
+       JsonValue *j_uploads = (JsonValue *) userdata;
+       JsonArrayAppend(j_uploads, NewJsonNumber(HKEY("foo"), 1));
+       JsonArrayAppend(j_uploads, NewJsonNumber(HKEY("bar"), 2));
+       JsonArrayAppend(j_uploads, NewJsonNumber(HKEY("baz"), 3));
 }
 
 // upload handler
 void upload_files(struct http_transaction *h, struct ctdlsession *c) {
        // FIXME reject uploads if we're not logged in
 
+       // This will be a JSON Array of all files that were uploaded during this HTTP transaction.
+       // Normally the browser will upload only one file per transaction, but that behavior is not guaranteed.
+       JsonValue *j_uploads = NewJsonArray(HKEY(""));
+
        // h->request_body will contain the upload(s) in MIME format
-       mime_parser(h->request_body, (h->request_body + h->request_body_length), *upload_handler, NULL, NULL, NULL, 0);
+       mime_parser(h->request_body, (h->request_body + h->request_body_length), *upload_handler, NULL, NULL, j_uploads, 0);
 
        // probably do something more clever here
        h->response_code = 200;
        h->response_string = strdup("OK");
+
+       // send back a JSON array of all files uploaded
+       StrBuf *sj = NewStrBuf();
+       SerializeJson(sj, j_uploads, 1);        // '1' == free the source object
        add_response_header(h, strdup("Content-type"), strdup("application/json"));
-       h->response_body = "{}";
-       h->response_body_length = strlen(h->response_body);
+       h->response_code = 200;
+       h->response_string = strdup("OK");
+       h->response_body_length = StrLength(sj);
+       h->response_body = SmashStrBuf(&sj);
 }
\ No newline at end of file