blob: 34beefae11cd414c0c1c0efc3ead487b23fe2287 [file] [log] [blame]
joshualitt3854f112016-02-25 11:28:18 -08001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "UrlHandler.h"
9
10#include "microhttpd.h"
11#include "../Request.h"
12#include "../Response.h"
13
14using namespace Response;
15
16static const size_t kBufferSize = 1024;
17
18static int process_upload_data(void* cls, enum MHD_ValueKind kind,
19 const char* key, const char* filename,
20 const char* content_type, const char* transfer_encoding,
21 const char* data, uint64_t off, size_t size) {
22 struct UploadContext* uc = reinterpret_cast<UploadContext*>(cls);
23
24 if (0 != size) {
25 uc->fStream.write(data, size);
26 }
27 return MHD_YES;
28}
29
30bool PostHandler::canHandle(const char* method, const char* url) {
31 return 0 == strcmp(method, MHD_HTTP_METHOD_POST) &&
32 0 == strcmp(url, "/new");
33}
34
35int PostHandler::handle(Request* request, MHD_Connection* connection,
36 const char* url, const char* method,
37 const char* upload_data, size_t* upload_data_size) {
38 UploadContext* uc = request->fUploadContext;
39
40 // New connection
41 if (!uc) {
42 // TODO make this a method on request
43 uc = new UploadContext;
44 uc->connection = connection;
45 uc->fPostProcessor = MHD_create_post_processor(connection, kBufferSize,
46 &process_upload_data, uc);
47 SkASSERT(uc->fPostProcessor);
48
49 request->fUploadContext = uc;
50 return MHD_YES;
51 }
52
53 // in process upload
54 if (0 != *upload_data_size) {
55 SkASSERT(uc->fPostProcessor);
56 MHD_post_process(uc->fPostProcessor, upload_data, *upload_data_size);
57 *upload_data_size = 0;
58 return MHD_YES;
59 }
60
61 // end of upload
62 MHD_destroy_post_processor(uc->fPostProcessor);
63 uc->fPostProcessor = nullptr;
64
joshualitt6bc96792016-02-29 05:35:04 -080065 if (!request->initPictureFromStream(request->fUploadContext->fStream.detachAsStream())) {
joshualitt3854f112016-02-25 11:28:18 -080066 fprintf(stderr, "Could not create picture from stream.\n");
67 return MHD_NO;
68 }
halcanary9d524f22016-03-29 09:03:52 -070069
joshualitt3854f112016-02-25 11:28:18 -080070 // clear upload context
71 delete request->fUploadContext;
72 request->fUploadContext = nullptr;
73
74 return SendTemplate(connection, true, "/");
75}