Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(354)

Side by Side Diff: tools/skiaserve/skiaserve.cpp

Issue 1653203002: cleanup of skia serve url handling (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2016 Google Inc. 2 * Copyright 2016 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "GrCaps.h" 8 #include "GrCaps.h"
9 #include "GrContextFactory.h" 9 #include "GrContextFactory.h"
10 #include "SkCanvas.h" 10 #include "SkCanvas.h"
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 if (redirect) { 165 if (redirect) {
166 MHD_add_response_header (response, "Location", redirectUrl); 166 MHD_add_response_header (response, "Location", redirectUrl);
167 status = MHD_HTTP_SEE_OTHER; 167 status = MHD_HTTP_SEE_OTHER;
168 } 168 }
169 169
170 int ret = MHD_queue_response(connection, status, response); 170 int ret = MHD_queue_response(connection, status, response);
171 MHD_destroy_response(response); 171 MHD_destroy_response(response);
172 return ret; 172 return ret;
173 } 173 }
174 174
175 typedef int (*UrlHandler)(Request* request, MHD_Connection* connection, 175 class UrlHandler {
176 const char* upload_data, size_t* upload_data_size); 176 public:
177 virtual ~UrlHandler() {}
178 virtual bool canHandle(const char* method, const char* url) = 0;
179 virtual int handle(Request* request, MHD_Connection* connection,
180 const char* upload_data, size_t* upload_data_size) = 0;
181 };
177 182
178 int rootHandler(Request* request, MHD_Connection* connection, 183 class InfoHandler : public UrlHandler {
179 const char* upload_data, size_t* upload_data_size) { 184 public:
180 return SendTemplate(connection); 185 bool canHandle(const char* method, const char* url) override {
181 } 186 return 0 == strcmp(method, MHD_HTTP_METHOD_GET) &&
182 187 0 == strcmp(url, "/cmd");
183 int postHandler(Request* request, MHD_Connection* connection,
184 const char* upload_data, size_t* upload_data_size) {
185 UploadContext* uc = request->fUploadContext;
186
187 // New connection
188 if (!uc) {
189 // TODO make this a method on request
190 uc = new UploadContext;
191 uc->connection = connection;
192 uc->fPostProcessor = MHD_create_post_processor(connection, kBufferSize,
193 &process_upload_data, uc) ;
194 SkASSERT(uc->fPostProcessor);
195
196 request->fUploadContext = uc;
197 return MHD_YES;
198 } 188 }
199 189
200 // in process upload 190 int handle(Request* request, MHD_Connection* connection,
201 if (0 != *upload_data_size) { 191 const char* upload_data, size_t* upload_data_size) override {
202 SkASSERT(uc->fPostProcessor); 192 if (request->fPicture.get()) {
203 MHD_post_process(uc->fPostProcessor, upload_data, *upload_data_size); 193 return SendJSON(connection, request->fPicture);
204 *upload_data_size = 0; 194 }
205 return MHD_YES; 195 return MHD_NO;
196 }
197 };
198
199 class ImgHandler : public UrlHandler {
200 public:
201 bool canHandle(const char* method, const char* url) override {
202 return 0 == strcmp(method, MHD_HTTP_METHOD_GET) &&
203 0 == strcmp(url, "/img");
jcgregorio 2016/02/02 12:35:31 0 == strncmp(url, "/img", 4); So the cache-bustin
joshualitt 2016/02/02 14:02:34 Acknowledged.
206 } 204 }
207 205
208 // end of upload 206 int handle(Request* request, MHD_Connection* connection,
209 MHD_destroy_post_processor(uc->fPostProcessor); 207 const char* upload_data, size_t* upload_data_size) override {
210 uc->fPostProcessor = nullptr; 208 if (request->fPNG.get()) {
209 SkData* data = request->fPNG.get();
210 return SendData(connection, data, "image/png");
211 }
212 return MHD_NO;
213 }
214 };
211 215
212 // TODO response 216 class PostHandler : public UrlHandler {
213 SkString error; 217 public:
214 if (!setupAndDrawToCanvas(request, &error)) { 218 bool canHandle(const char* method, const char* url) override {
215 // TODO send error 219 return 0 == strcmp(method, MHD_HTTP_METHOD_POST) &&
216 return MHD_YES; 220 0 == strcmp(url, "/new");
217 } 221 }
218 222
219 return SendTemplate(connection, true, "/"); 223 int handle(Request* request, MHD_Connection* connection,
220 } 224 const char* upload_data, size_t* upload_data_size) override {
225 UploadContext* uc = request->fUploadContext;
221 226
222 int imgHandler(Request* request, MHD_Connection* connection, 227 // New connection
223 const char* upload_data, size_t* upload_data_size) { 228 if (!uc) {
224 if (request->fPNG.get()) { 229 // TODO make this a method on request
225 SkData* data = request->fPNG.get(); 230 uc = new UploadContext;
226 return SendData(connection, data, "image/png"); 231 uc->connection = connection;
232 uc->fPostProcessor = MHD_create_post_processor(connection, kBufferSi ze,
233 &process_upload_data, uc);
234 SkASSERT(uc->fPostProcessor);
235
236 request->fUploadContext = uc;
237 return MHD_YES;
238 }
239
240 // in process upload
241 if (0 != *upload_data_size) {
242 SkASSERT(uc->fPostProcessor);
243 MHD_post_process(uc->fPostProcessor, upload_data, *upload_data_size) ;
244 *upload_data_size = 0;
245 return MHD_YES;
246 }
247
248 // end of upload
249 MHD_destroy_post_processor(uc->fPostProcessor);
250 uc->fPostProcessor = nullptr;
251
252 // TODO response
253 SkString error;
254 if (!setupAndDrawToCanvas(request, &error)) {
255 // TODO send error
256 return MHD_YES;
257 }
258
259 return SendTemplate(connection, true, "/");
227 } 260 }
228 return MHD_NO; 261 };
229 }
230 262
231 int infoHandler(Request* request, MHD_Connection* connection, 263 class RootHandler : public UrlHandler {
232 const char* upload_data, size_t* upload_data_size) { 264 public:
233 if (request->fPicture.get()) { 265 bool canHandle(const char* method, const char* url) override {
234 return SendJSON(connection, request->fPicture); 266 return 0 == strcmp(method, MHD_HTTP_METHOD_GET) &&
267 0 == strcmp(url, "/");
235 } 268 }
236 return MHD_NO; 269
237 } 270 int handle(Request* request, MHD_Connection* connection,
271 const char* upload_data, size_t* upload_data_size) override {
272 return SendTemplate(connection);
273 }
274 };
238 275
239 class UrlManager { 276 class UrlManager {
240 public: 277 public:
241 UrlManager() { 278 UrlManager() {
242 // Register handlers 279 // Register handlers
243 fHandlers.push_back({MHD_HTTP_METHOD_GET, "/", rootHandler}); 280 fHandlers.push_back(new RootHandler);
244 fHandlers.push_back({MHD_HTTP_METHOD_POST, "/new", postHandler}); 281 fHandlers.push_back(new PostHandler);
245 fHandlers.push_back({MHD_HTTP_METHOD_GET, "/img", imgHandler}); 282 fHandlers.push_back(new ImgHandler);
246 fHandlers.push_back({MHD_HTTP_METHOD_GET, "/cmd", infoHandler}); 283 fHandlers.push_back(new InfoHandler);
284 }
285
286 ~UrlManager() {
287 for (int i = 0; i < fHandlers.count(); i++) { delete fHandlers[i]; }
247 } 288 }
248 289
249 // This is clearly not efficient for a large number of urls and handlers 290 // This is clearly not efficient for a large number of urls and handlers
250 int invoke(Request* request, MHD_Connection* connection, const char* url, co nst char* method, 291 int invoke(Request* request, MHD_Connection* connection, const char* url, co nst char* method,
251 const char* upload_data, size_t* upload_data_size) const { 292 const char* upload_data, size_t* upload_data_size) const {
252 for (int i = 0; i < fHandlers.count(); i++) { 293 for (int i = 0; i < fHandlers.count(); i++) {
253 const Url& urlHandler = fHandlers[i]; 294 if (fHandlers[i]->canHandle(method, url)) {
254 if (0 == strcmp(method, urlHandler.fMethod) && 295 return fHandlers[i]->handle(request, connection, upload_data, up load_data_size);
jcgregorio 2016/02/02 12:35:31 Please log the method and the url to stderr.
joshualitt 2016/02/02 14:02:34 Acknowledged.
255 0 == strcmp(url, urlHandler.fPath)) {
256 return (*urlHandler.fHandler)(request, connection, upload_da ta,
257 upload_data_size);
258 } 296 }
259 } 297 }
260 return MHD_NO; 298 return MHD_NO;
261 } 299 }
262 300
263 private: 301 private:
264 struct Url { 302 SkTArray<UrlHandler*> fHandlers;
265 const char* fMethod;
266 const char* fPath;
267 UrlHandler fHandler;
268 };
269 SkTArray<Url> fHandlers;
270 }; 303 };
271 304
272 const UrlManager kUrlManager; 305 const UrlManager kUrlManager;
273 306
274 int answer_to_connection(void* cls, struct MHD_Connection* connection, 307 int answer_to_connection(void* cls, struct MHD_Connection* connection,
275 const char* url, const char* method, const char* versio n, 308 const char* url, const char* method, const char* versio n,
276 const char* upload_data, size_t* upload_data_size, 309 const char* upload_data, size_t* upload_data_size,
277 void** con_cls) { 310 void** con_cls) {
278 SkDebugf("New %s request for %s using version %s\n", method, url, version); 311 SkDebugf("New %s request for %s using version %s\n", method, url, version);
279 312
(...skipping 16 matching lines...) Expand all
296 MHD_stop_daemon(daemon); 329 MHD_stop_daemon(daemon);
297 return 0; 330 return 0;
298 } 331 }
299 332
300 #if !defined SK_BUILD_FOR_IOS 333 #if !defined SK_BUILD_FOR_IOS
301 int main(int argc, char** argv) { 334 int main(int argc, char** argv) {
302 SkCommandLineFlags::Parse(argc, argv); 335 SkCommandLineFlags::Parse(argc, argv);
303 return skiaserve_main(); 336 return skiaserve_main();
304 } 337 }
305 #endif 338 #endif
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698