Skip to content

Commit

Permalink
Allow remote upload (preflighted requests)
Browse files Browse the repository at this point in the history
Implement preflighted requests (OPTIONS) to allow remote file upload
(CORS).

-
https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Preflighted_requests

- https://www.w3.org/TR/cors/
  • Loading branch information
skarab42 committed Apr 17, 2016
1 parent 12cfaea commit 2974639
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/libs/Network/uip/webserver/http-strings
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ http_301 "301 "
http_302 "302 "
http_get "GET "
http_post "POST "
http_options "OPTIONS "
http_10 "HTTP/1.0"
http_11 "HTTP/1.1"
http_content_type "content-type: "
Expand Down
3 changes: 3 additions & 0 deletions src/libs/Network/uip/webserver/http-strings.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const char http_get[5] =
const char http_post[6] =
/* "POST " */
{0x50, 0x4f, 0x53, 0x54, 0x20, };
const char http_options[9] =
/* "OPTIONS " */
{0x4f, 0x50, 0x54, 0x49, 0x4f, 0x4e, 0x53, 0x20, };
const char http_10[9] =
/* "HTTP/1.0" */
{0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x30, };
Expand Down
1 change: 1 addition & 0 deletions src/libs/Network/uip/webserver/http-strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extern const char http_301[5];
extern const char http_302[5];
extern const char http_get[5];
extern const char http_post[6];
extern const char http_options[9];
extern const char http_10[9];
extern const char http_11[9];
extern const char http_content_type[15];
Expand Down
22 changes: 16 additions & 6 deletions src/libs/Network/uip/webserver/httpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@
#define STATE_OUTPUT 3
#define STATE_UPLOAD 4

#define GET 1
#define POST 2
#define GET 1
#define POST 2
#define OPTIONS 3

#define ISO_nl 0x0a
#define ISO_space 0x20
Expand Down Expand Up @@ -324,7 +325,11 @@ PT_THREAD(handle_output(struct httpd_state *s))
{
PT_BEGIN(&s->outputpt);

if (s->method == POST) {
if (s->method == OPTIONS) {
PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_preflight));
PSOCK_SEND_STR(&s->sout, "OK\r\n");
}
else if (s->method == POST) {
if (strcmp(s->filename, "/command") == 0) {
DEBUG_PRINTF("Executed command post\n");
PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_200));
Expand Down Expand Up @@ -471,16 +476,18 @@ PT_THREAD(handle_input(struct httpd_state *s))

PSOCK_READTO(&s->sin, ISO_space);

if (strncmp(s->inputbuf, http_get, 4) == 0) {
if (strncmp(s->inputbuf, http_get, 3) == 0) {
s->method = GET;
} else if (strncmp(s->inputbuf, http_post, 4) == 0) {
s->method = POST;
} else if (strncmp(s->inputbuf, http_options, 7) == 0) {
s->method = OPTIONS;
} else {
DEBUG_PRINTF("Unexpected method: %s\n", s->inputbuf);
PSOCK_CLOSE_EXIT(&s->sin);
}

DEBUG_PRINTF("Method: %s\n", s->method == POST ? "POST" : "GET");
DEBUG_PRINTF("Method: %s\n", s->method == POST ? "POST" : (s->method == GET ? "GET" : "OPTIONS"));

PSOCK_READTO(&s->sin, ISO_space);

Expand Down Expand Up @@ -509,7 +516,10 @@ PT_THREAD(handle_input(struct httpd_state *s))
s->inputbuf[PSOCK_DATALEN(&s->sin) - 1] = 0;
if (s->inputbuf[0] == '\r') {
DEBUG_PRINTF("end of headers\n");
if (s->method == GET) {
if (s->method == OPTIONS) {
s->state = STATE_OUTPUT;
break;
} else if (s->method == GET) {
s->state = STATE_OUTPUT;
break;
} else if (s->method == POST) {
Expand Down

0 comments on commit 2974639

Please sign in to comment.