comparison mod_http_upload/mod_http_upload.lua @ 3376:972832108c78

mod_http_upload: Add CORS headers for web clients (untested)
author Matthew Wild <mwild1@gmail.com>
date Sun, 18 Nov 2018 12:06:13 +0000
parents fca95f1b8870
children 683365d370d8
comparison
equal deleted inserted replaced
3375:6317a5d8ce2d 3376:972832108c78
282 t[k] = v; 282 t[k] = v;
283 return v; 283 return v;
284 end 284 end
285 }); 285 });
286 286
287 local function set_cross_domain_headers(response)
288 local headers = response.headers;
289 headers.access_control_allow_methods = "GET, PUT, POST, OPTIONS";
290 headers.access_control_allow_headers = "Content-Type";
291 headers.access_control_max_age = "7200";
292 headers.access_control_allow_origin = response.request.headers.origin or "*";
293 return response;
294 end
295
287 local function send_response_sans_body(response, body) 296 local function send_response_sans_body(response, body)
288 if response.finished then return; end 297 if response.finished then return; end
289 response.finished = true; 298 response.finished = true;
290 response.conn._http_open_response = nil; 299 response.conn._http_open_response = nil;
291 300
318 end 327 end
319 328
320 local serve_uploaded_files = http_files.serve(storage_path); 329 local serve_uploaded_files = http_files.serve(storage_path);
321 330
322 local function serve_head(event, path) 331 local function serve_head(event, path)
332 set_cross_domain_headers(event.response);
323 event.response.send = send_response_sans_body; 333 event.response.send = send_response_sans_body;
324 event.response.send_file = send_response_sans_body; 334 event.response.send_file = send_response_sans_body;
325 return serve_uploaded_files(event, path); 335 return serve_uploaded_files(event, path);
326 end 336 end
327 337
335 ["GET"] = serve_hello; 345 ["GET"] = serve_hello;
336 ["GET /"] = serve_hello; 346 ["GET /"] = serve_hello;
337 ["GET /*"] = serve_uploaded_files; 347 ["GET /*"] = serve_uploaded_files;
338 ["HEAD /*"] = serve_head; 348 ["HEAD /*"] = serve_head;
339 ["PUT /*"] = upload_data; 349 ["PUT /*"] = upload_data;
350
351 ["OPTIONS /*"] = function (event)
352 if event.request.headers.origin then
353 set_cross_domain_headers(event.response);
354 end
355 return "";
356 end;
340 }; 357 };
341 }); 358 });
342 359
343 module:log("info", "URL: <%s>; Storage path: %s", module:http_url(), storage_path); 360 module:log("info", "URL: <%s>; Storage path: %s", module:http_url(), storage_path);
344 361