diff --git a/httpd/event.h b/httpd/event.h index e415ef4..c314bd8 100644 --- a/httpd/event.h +++ b/httpd/event.h @@ -1,6 +1,7 @@ #ifndef __EVENT_H__ #define __EVENT_H__ +#define BOUNDARY_MAX_LEN 64 typedef enum { EV_UNKNOWN = 0x00, @@ -12,17 +13,17 @@ typedef enum typedef enum { - evIdle = 0, - evBusy, - evFinish + EV_IDLE = 0, + EV_BUSY, + EV_FINISH } event_status_t; typedef struct { char file[MAX_PATH]; + char boundary[BOUNDARY_MAX_LEN]; uint32_t total; uint32_t offset; - uint32_t tail; uint32_t size; FILE *fp; // fixed fopen EACCES error. just for write file char data[1]; diff --git a/httpd/http.c b/httpd/http.c index 445a69b..3699df1 100644 --- a/httpd/http.c +++ b/httpd/http.c @@ -25,25 +25,31 @@ static void accept_callback(event_t *ev); static void read_callback(event_t *ev); static void write_callback(event_t *ev); -static int read_request_header(event_t *ev, char **buf, int *size); -static int parse_request_header(char *data, request_header_t *header); -static void release_request_header(request_header_t *header); -static void release_event(event_t *ev); +static int read_request_header(event_t *ev, char **buf, int *size); +static void read_request_boundary(event_t *ev); +static int parse_request_header(char *data, request_header_t *header); +static void release_request_header(request_header_t *header); +static void release_event(event_t *ev); static event_data_t *create_event_data(const char *header, const char *html); static event_data_t *create_event_data_fp(const char *header, FILE *fp, int read_len, int total_len); -static void release_event_data(event_t *ev); -static void uri_decode(char* uri); +static void release_event_data(event_t *ev); +static void uri_decode(char* uri); static uint8_t ishex(uint8_t x); -static char* local_file_list(char *path); +static char *local_file_list(char *path); +static int reset_filename_from_formdata(event_t *ev, char **formdata, int size); +static int parse_boundary(event_t *ev, char *data, int size, char **ptr); static const char *reponse_content_type(char *file_name); static const char *response_header_format(); +static const char *response_body_format(); static void response_home_page(event_t *ev, char *path); static void response_upload_page(event_t *ev, int result); static void response_send_file_page(event_t *ev, char *file_name); static void response_http_400_page(event_t *ev); static void response_http_404_page(event_t *ev); +static void response_http_500_page(event_t *ev); static void response_http_501_page(event_t *ev); +static void send_response(event_t *ev, char* title, char *status); int http_startup(uint16_t *port) { @@ -95,19 +101,14 @@ static void accept_callback(event_t *ev) static void read_callback(event_t *ev) { char *buf = NULL; - int size; + int size; request_header_t header; - int i; - int content_length = 0; - int boundary_length = 0; - char *temp = NULL, *p; - char file_name[MAX_PATH] = {0}; - int len, ret, write_len; - char buffer[BUFFER_UNIT+1] = {0}; - event_data_t* ev_data = NULL; - FILE* fp = NULL; + int i; + int content_length = 0; + char *temp = NULL; + char file_path[MAX_PATH] = {0}; - if (ev->status == evIdle) + if (ev->status == EV_IDLE) { if (SUCC != read_request_header(ev, &buf, &size)) { @@ -117,18 +118,16 @@ static void read_callback(event_t *ev) } if (!buf) return; - //log_debug("%s", buf); parse_request_header(buf, &header); uri_decode(header.uri); header.uri = utf8_to_ansi(header.uri); - log_debug("{%s:%d} >>> Entry recv ... uri=%s", __FUNCTION__, __LINE__, header.uri); + log_info("{%s:%d} >>> Entry recv ... uri=%s", __FUNCTION__, __LINE__, header.uri); if (strcmp(header.method, "GET") && strcmp(header.method, "POST")) { // 501 Not Implemented response_http_501_page(ev); - free(header.uri); - free(buf); release_request_header(&header); + free(buf); return; } if (0 == strcmp(header.method, "POST")) @@ -145,11 +144,11 @@ static void read_callback(event_t *ev) if (0 == strncmp(header.uri, "/upload", strlen("/upload"))) { // get upload file save path from uri - memset(file_name, 0, sizeof(file_name)); - memcpy(file_name, root_path(), strlen(root_path())); + memset(file_path, 0, sizeof(file_path)); + memcpy(file_path, root_path(), strlen(root_path())); if (strlen(header.uri) > strlen("/upload?path=")) { - memcpy(file_name+strlen(file_name), header.uri+strlen("/upload?path="), strlen(header.uri)-strlen("/upload?path=")); + memcpy(file_path+strlen(file_path), header.uri+strlen("/upload?path="), strlen(header.uri)-strlen("/upload?path=")); } if (0 == strcmp(header.method, "POST")) { @@ -167,13 +166,22 @@ static void read_callback(event_t *ev) // not support // 501 Not Implemented response_http_501_page(ev); - free(header.uri); - free(buf); release_request_header(&header); + free(buf); return; } - // get boundary_length + // get boundary + ev->data = (event_data_t*)malloc(sizeof(event_data_t)-sizeof(char)+BUFFER_UNIT); + memset(ev->data, 0, sizeof(event_data_t)-sizeof(char)+BUFFER_UNIT); + if (!ev->data) + { + // 500 Internal Server Error + response_http_500_page(ev); + release_request_header(&header); + free(buf); + return; + } for (i=0; idata->boundary, temp, strlen(temp)); } break; } } - if (boundary_length == 0) + if (ev->data->boundary[0] == 0) { // not support // 501 Not Implemented response_http_501_page(ev); - free(header.uri); - free(buf); release_request_header(&header); - return; - } - - // read boundary - free(header.uri); - free(buf);buf=NULL; - release_request_header(&header); - if (SUCC != read_request_header(ev, &buf, &size)) - { - response_http_400_page(ev); - return; - } - //log_debug("%s", buf); - len = strlen(buf); - - // get upload filename - temp = strstr(buf, "filename=\""); - if (!temp) - { - response_http_400_page(ev); free(buf); return; } - temp += strlen("filename=\""); - *strstr(temp, "\"") = 0; - // IE browser: remove client file path - p = temp; - while (*p) - { - if (*p == '\\' || *p == '/') - if (*(p+1)) - temp = p+1; - p++; - } - memcpy(file_name+strlen(file_name), temp, strlen(temp)); - log_error("{%s:%d} Upload file name = %s", __FUNCTION__, __LINE__, file_name); - // if file exist delete file - if (file_exist(file_name)) - { - remove_file(file_name); - } - - // begin read file - ev_data = (event_data_t*)malloc(sizeof(event_data_t)); - memset(ev_data, 0, sizeof(event_data_t)); - memcpy(ev_data->file, file_name, strlen(file_name)); - ev_data->total = content_length; - ev_data->offset = len; - ev_data->tail = boundary_length + strlen(CRLF)*3+strlen("--"); - ASSERT(content_length > len); - ev->status = evBusy; - ev->data = ev_data; + // relase memory + release_request_header(&header); free(buf); - buf = NULL; - return; + + // set event + memcpy(ev->data->file, file_path, strlen(file_path)); + ev->data->offset = 0; + ev->data->total = content_length; + + // read & save files + read_request_boundary(ev); } else { @@ -265,7 +230,6 @@ static void read_callback(event_t *ev) else if (header.uri[strlen(header.uri)-1] == '/') { response_home_page(ev, header.uri+1); - free(header.uri); free(buf); release_request_header(&header); return; @@ -273,10 +237,10 @@ static void read_callback(event_t *ev) else { // send file - memset(file_name, 0, sizeof(file_name)); - memcpy(file_name, root_path(), strlen(root_path())); - memcpy(file_name+strlen(file_name), header.uri+1, strlen(header.uri+1)); - response_send_file_page(ev, file_name); + memset(file_path, 0, sizeof(file_path)); + memcpy(file_path, root_path(), strlen(root_path())); + memcpy(file_path+strlen(file_path), header.uri+1, strlen(header.uri+1)); + response_send_file_page(ev, file_path); free(buf); release_request_header(&header); return; @@ -284,62 +248,8 @@ static void read_callback(event_t *ev) } else { - len = ev->data->total - ev->data->offset > BUFFER_UNIT ? BUFFER_UNIT : ev->data->total - ev->data->offset; - ret = network_read(ev->fd, buffer, len); - if (ret == DISC) - { - release_event(ev); - return; - } - else if (ret == SUCC) - { - write_len = ev->data->total - ev->data->offset - len > ev->data->tail ? len : ev->data->total - ev->data->offset - ev->data->tail; - if (write_len > 0) - { - fp = ev->data->fp; - if (!fp) - { - fp = fopen(ev->data->file, "ab"); - if (!fp) - { - log_error("{%s:%d} open file fail. filename=%s, socket=%d, errno=%d", __FUNCTION__, __LINE__, ev->data->file, ev->fd, errno); - release_event_data(ev); - ev->status = evIdle; - response_upload_page(ev, 0); - return; - } - ev->data->fp = fp; - } - if (write_len != fwrite(buffer, 1, write_len, fp)) - { - log_error("{%s:%d} write file fail. filename=%s, socket=%d, errno=%d", __FUNCTION__, __LINE__, ev->data->file, ev->fd, errno); - release_event_data(ev); - ev->status = evIdle; - response_upload_page(ev, 0); - return; - } - //fclose(fp); fixed fopen EACCES error - } - - ev->data->offset += len; - log_debug("{%s:%d} upload file progress=%d%%. socket=%d", __FUNCTION__, __LINE__,ev->data->offset*100/ev->data->total, ev->fd); - if (ev->data->offset == ev->data->total) - { - release_event_data(ev); - ev->status = evIdle; - response_upload_page(ev, 1); - log_info("{%s:%d} upload file complete. socket=%d", __FUNCTION__, __LINE__, ev->fd); - } - return; - } - else - { - log_error("{%s:%d} recv unknown fail.", __FUNCTION__, __LINE__); - release_event_data(ev); - ev->status = evIdle; - response_upload_page(ev, 0); - return; - } + // read & save files + read_request_boundary(ev); } } @@ -358,6 +268,7 @@ static void write_callback(event_t *ev) log_debug("{%s:%d} send response completed. progress=%d%%, socket=%d", __FUNCTION__, __LINE__, ev->data->total ? ev->data->offset*100/ev->data->total : 100, ev->fd); if (ev->data->total == ev->data->offset) { + log_info("{%s:%d} send response completed. socket=%d", __FUNCTION__, __LINE__, ev->fd); release_event_data(ev); } else @@ -423,6 +334,144 @@ static int read_request_header(event_t *ev, char **buf, int *size) return FAIL; } +static void read_request_boundary(event_t *ev) +{ +#define WRITE_FILE(fp, buf, size, ev) do { \ + if (size) { \ + if (size != fwrite(compare_buff, 1, size, fp)) { \ + log_error("{%s:%d} write file fail. socket=%d", __FUNCTION__, __LINE__, ev->fd); \ + release_event_data(ev); \ + ev->status = EV_IDLE; \ + response_upload_page(ev, 0); \ + return; \ + } \ + } \ +} while (0) + +#define GET_FILENAME(ev, ptr, size_) do { \ + ret = reset_filename_from_formdata(ev, &ptr, (size_)); \ + if (ret == 0) { \ + log_error("{%s:%d} cannot found filename in formdata. socket=%d", __FUNCTION__, __LINE__, ev->fd); \ + release_event_data(ev); \ + ev->status = EV_IDLE; \ + response_upload_page(ev, 0); \ + return; \ + } else if (ret == 1) { \ + ev->data->fp = fopen(ev->data->file, "wb"); \ + if (!ev->data->fp) { \ + log_error("{%s:%d} open file fail. filename=%s, socket=%d, errno=%d", __FUNCTION__, __LINE__, ev->data->file, ev->fd, errno); \ + release_event_data(ev); \ + ev->status = EV_IDLE; \ + response_upload_page(ev, 0); \ + return; \ + } \ + compare_buff_size -= ptr - compare_buff; \ + memcpy(compare_buff, ptr, compare_buff_size); \ + compare_buff[compare_buff_size] = 0; \ + goto _re_find; \ + } else { \ + ev->data->size = compare_buff + compare_buff_size - ptr; \ + memcpy(ev->data->data, ptr, ev->data->size); \ + } \ +} while (0) + + char *buf = NULL; + char *ptr = NULL; + int ret = 0; + uint32_t offset = 0; + uint32_t writen = 0; + uint32_t compare_buff_size = 0; + char buffer[BUFFER_UNIT+1] = {0}; + char compare_buff[BUFFER_UNIT*2 + 1] = {0}; + + offset = ev->data->total - ev->data->offset > BUFFER_UNIT ? BUFFER_UNIT : ev->data->total - ev->data->offset; + ret = network_read(ev->fd, buffer, offset); + if (ret == DISC) + { + response_http_500_page(ev); + release_event(ev); + return; + } + else if (ret == SUCC) + { + memset(compare_buff, 0, sizeof(compare_buff)); + if (ev->data->size) + { + memcpy(compare_buff, ev->data->data, ev->data->size); + } + memcpy(compare_buff+ev->data->size, buffer, offset); + compare_buff_size = offset + ev->data->size; + ev->data->size = 0; + memset(ev->data->data, 0, BUFFER_UNIT); + + // parse boundary + _re_find: + ret = parse_boundary(ev, compare_buff, compare_buff_size, &ptr); + switch (ret) + { + case 0: // write all bytes to file + WRITE_FILE(ev->data->fp, compare_buff, compare_buff_size, ev); + log_debug("{%s:%d} upload [%s] progress=%d%%. socket=%d", __FUNCTION__, __LINE__, ev->data->file, ev->data->offset * 100 / ev->data->total, ev->fd); + break; + case 1: // first boundary + // get file name from boundary header + GET_FILENAME(ev, ptr, compare_buff + compare_buff_size - ptr); + break; + case 2: // last boundary + ASSERT(ev->data->total == ev->data->offset + offset); + writen = ptr - compare_buff; + // writen bytes before boundary + WRITE_FILE(ev->data->fp, compare_buff, writen, ev); + fclose(ev->data->fp); + log_info("{%s:%d} upload [%s] complete. socket=%d", __FUNCTION__, __LINE__, ev->data->file, ev->fd); + release_event_data(ev); + ev->status = EV_IDLE; + response_upload_page(ev, 1); + return; + case 3: // middle boundary + // writen bytes before boundary + writen = ptr - compare_buff; + WRITE_FILE(ev->data->fp, compare_buff, writen, ev); + fclose(ev->data->fp); + log_info("{%s:%d} upload [%s] complete. socket=%d", __FUNCTION__, __LINE__, ev->data->file, ev->fd); + // get file name from boundary header + GET_FILENAME(ev, ptr, compare_buff + compare_buff_size - ptr); + break; + case 4: // backup last boundary + case 5: // backup middle boundary + writen = ptr - compare_buff; + if (writen) + { + if (writen != fwrite(compare_buff, 1, writen, ev->data->fp)) + { + log_error("{%s:%d} write file fail. socket=%d", __FUNCTION__, __LINE__, ev->fd); + release_event_data(ev); + ev->status = EV_IDLE; + response_upload_page(ev, 0); + return; + } + } + // backup + ev->data->size = compare_buff + compare_buff_size - ptr; + memcpy(ev->data->data, ptr, ev->data->size); + break; + default: + break; + } + + ev->data->offset += offset; + ev->status = EV_BUSY; + } + else + { + log_error("{%s:%d} recv unknown fail.", __FUNCTION__, __LINE__); + release_event_data(ev); + ev->status = EV_IDLE; + response_upload_page(ev, 0); + return; + } +} + static int parse_request_header(char *data, request_header_t *header) { #define move_next_line(x) while (*x && *(x + 1) && *x != CR && *(x + 1) != LF) x++; @@ -492,6 +541,10 @@ static int parse_request_header(char *data, request_header_t *header) static void release_request_header(request_header_t *header) { + if (header->uri) + { + free(header->uri); + } if (header->fields) { free(header->fields); @@ -628,6 +681,151 @@ static uint8_t ishex(uint8_t x) (x >= 'A' && x <= 'F'); } +static int reset_filename_from_formdata(event_t *ev, char **formdata, int size) +{ + char *file_name = NULL; + char *p = NULL; + int i = 0; + int found = 0; + char *anis = NULL; + + // find "\r\n\r\n" + p = *formdata; + for (int i = 0; i <= size-4; i++) + { + if (0 == memcmp(*formdata + i, CRLF CRLF, strlen(CRLF CRLF))) + { + found = 1; + (*formdata)[i] = 0; + (*formdata) += i + strlen(CRLF CRLF); + break; + } + } + if (!found) + return 2; + + // file upload file name from formdata + file_name = strstr(p, "filename=\""); + if (!file_name) + return 0; + file_name += strlen("filename=\""); + *strstr(file_name, "\"") = 0; + + anis = utf8_to_ansi(file_name); + + // IE browser: remove client file path + p = anis; + while (*p) + { + if (*p == '\\' || *p == '/') + if (*(p + 1)) + anis = p + 1; + p++; + } + + // reset filepath + for (i = strlen(ev->data->file) - 1; i >= 0; i--) + { + if (ev->data->file[i] == '/') + { + memcpy(ev->data->file + i + 1, anis, strlen(anis) + 1); + break; + } + } + + // if file exist delete file + if (file_exist(ev->data->file)) + { + remove_file(ev->data->file); + } + free(anis); + return 1; +} + +static int parse_boundary(event_t *ev, char *data, int size, char **ptr) +{ + char first_boundary[BOUNDARY_MAX_LEN] = { 0 }; + char middle_boundary[BOUNDARY_MAX_LEN] = { 0 }; + char last_boundary[BOUNDARY_MAX_LEN] = { 0 }; + int first_len, middle_len, last_len; + int i; + + sprintf(first_boundary, "--%s\r\n", ev->data->boundary); //------WebKitFormBoundaryOG3Viw9MEZcexbvT\r\n + sprintf(middle_boundary, "\r\n--%s\r\n", ev->data->boundary); //\r\n----WebKitFormBoundaryOG3Viw9MEZcexbvT\r\n + sprintf(last_boundary, "\r\n--%s--\r\n", ev->data->boundary); //\r\n------WebKitFormBoundaryOG3Viw9MEZcexbvT--\r\n + first_len = strlen(first_boundary); + middle_len = strlen(middle_boundary); + last_len = strlen(last_boundary); + + ASSERT(size > first_len); + ASSERT(size > middle_len); + ASSERT(size > last_len); + if (0 == memcmp(data, first_boundary, first_len)) + { + *ptr = data + first_len; + return 1; // first boundary + } + + if (0 == memcmp(data + (size - last_len), last_boundary, last_len)) + { + *ptr = data + (size - last_len); + return 2; // last boundary + } + + for (i = 0; i < size; i++) + { + if (size - i >= last_len) + { + if (0 == memcmp(data + i, middle_boundary, middle_len)) + { + *ptr = data + i/* + middle_len*/; + return 3; // middle boundary + } + } + else if (size - i >= middle_len && size - i < last_len) + { + if (0 == memcmp(data + i, middle_boundary, middle_len)) + { + *ptr = data + i/* + middle_len*/; + return 3; // middle boundary + } + if (0 == memcmp(data + i, last_boundary, size - i)) + { + *ptr = data + i; + return 4; // backup last boundary + } + } + else if (size - i >= 7 && size - i < middle_len) + { + if (0 == memcmp(data + i, last_boundary, size - i)) + { + *ptr = data + i; + return 4; // backup last boundary + } + if (0 == memcmp(data + i, middle_boundary, size - i)) + { + *ptr = data + i; + return 5; // backup middle boundary + } + } + else + { + if (0 == memcmp(data + i, middle_boundary, size - i)) + { + *ptr = data + i; + return 5; // backup middle boundary + } + if (0 == memcmp(data + i, last_boundary, size - i)) + { + *ptr = data + i; + return 4; // backup last boundary + } + } + } + + return 0; +} + static char* local_file_list(char *path) { const char* format_dir = "%s/" CRLF; @@ -641,6 +839,7 @@ static char* local_file_list(char *path) int size = BUFFER_UNIT; int offset = 0; char *size_str = NULL; + char *utf8 = NULL; int i; sprintf(filter, "%s*", path); @@ -659,7 +858,8 @@ static char* local_file_list(char *path) { result = (char*)malloc(size); } - sprintf(line, format_dir, FindFileData.cFileName, FindFileData.cFileName); + utf8 = ansi_to_utf8(FindFileData.cFileName); + sprintf(line, format_dir, utf8, utf8); line_length = strlen(line); line[line_length] = 0; if (offset+line_length > size-1) @@ -669,6 +869,7 @@ static char* local_file_list(char *path) } memcpy(result+offset, line, line_length); offset += line_length; + free(utf8); } } while (FindNextFileA(hFind, &FindFileData)); FindClose(hFind); @@ -688,7 +889,8 @@ static char* local_file_list(char *path) { result = (char*)malloc(size); } - sprintf(line, format_file, FindFileData.cFileName, FindFileData.cFileName); + utf8 = ansi_to_utf8(FindFileData.cFileName); + sprintf(line, format_file, utf8, utf8); line_length = strlen(line); for (i=strlen(FindFileData.cFileName); i<60; i++) { @@ -708,6 +910,7 @@ static char* local_file_list(char *path) } memcpy(result+offset, line, line_length); offset += line_length; + free(utf8); } } while (FindNextFileA(hFind, &FindFileData)); FindClose(hFind); @@ -775,15 +978,30 @@ static const char *response_header_format() return http_header_format; } +static const char *response_body_format() +{ + const char *http_body_format = + "" CRLF + "%s" CRLF + "" CRLF + "

%s

" CRLF + ""; + + return http_body_format; +} + static void response_home_page(event_t *ev, char *path) { const char *html_format = "" CRLF - "Index of /%s" CRLF + "" CRLF + "" CRLF + "Index of /%s" CRLF + "" CRLF "" CRLF "

Index of /%s


" CRLF "
" CRLF - "" CRLF + "" CRLF "

" CRLF
         "%s" CRLF
         "";
@@ -794,19 +1012,22 @@ static void response_home_page(event_t *ev, char *path)
     char *file_list = NULL;
     char *html = NULL;
     event_t ev_ = {0};
+    char *utf8 = NULL;
 
+    utf8 = ansi_to_utf8(path);
     file_list = local_file_list(path);
     if (!file_list)
         return;
 
-    length = strlen(html_format) + strlen(file_list) + (strlen(path) - strlen("%s"))*3 + 1;
+    length = strlen(html_format) + strlen(file_list) + (strlen(utf8) - strlen("%s"))*3 + 1;
     html = (char*)malloc(length);
     if (!html)
     {
         log_error("{%s:%d} malloc fail.", __FUNCTION__, __LINE__);
         return;
     }
-    sprintf(html, html_format, path, path, path, file_list);
+    sprintf(html, html_format, utf8, utf8, utf8, file_list);
+    free(utf8);
     free(file_list);
     sprintf(header, response_header_format(), "200 OK", reponse_content_type(NULL), strlen(html));
     ev_data = create_event_data(header, html);
@@ -821,39 +1042,6 @@ static void response_home_page(event_t *ev, char *path)
     event_add(&ev_);
 }
 
-static void response_upload_page(event_t *ev, int result)
-{
-    const char *html_succ = 
-        "" CRLF
-        "Upload completed" CRLF
-        "" CRLF
-        "

Upload completed!

" CRLF - ""; - - const char *html_fail = - "" CRLF - "Upload failed" CRLF - "" CRLF - "

Upload failed!

" CRLF - ""; - - char header[BUFFER_UNIT] = { 0 }; - int len = result ? strlen(html_succ) : strlen(html_fail); - event_data_t* ev_data = NULL; - event_t ev_ = {0}; - - sprintf(header, response_header_format(), "200 OK", reponse_content_type(NULL), len); - ev_data = create_event_data(header, result ? html_succ : html_fail); - - ev_.fd = ev->fd; - ev_.ip = ev->ip; - ev_.type = EV_WRITE; - ev_.param = ev->param; - ev_.data = ev_data; - ev_.callback = write_callback; - event_add(&ev_); -} - static void response_send_file_page(event_t *ev, char *file_name) { char header[BUFFER_UNIT] = { 0 }; @@ -905,77 +1093,48 @@ static void response_send_file_page(event_t *ev, char *file_name) fclose(fp); } -static void response_http_400_page(event_t *ev) +static void response_upload_page(event_t *ev, int result) { - const char *http_error_400_page = - "" CRLF - "400 Bad Request" CRLF - "" CRLF - "

400 Bad Request

" CRLF - ""; - - char header[BUFFER_UNIT] = { 0 }; - event_data_t* ev_data = NULL; - int len; - event_t ev_ = {0}; - - len = strlen(http_error_400_page); - sprintf(header, response_header_format(), "400 Bad Request", reponse_content_type(NULL), len); - ev_data = create_event_data(header, http_error_400_page); + if (result) + { + send_response(ev, "Upload completed", "200 OK"); + } + else + { + send_response(ev, "Upload failed", "200 OK"); + } +} - ev_.fd = ev->fd; - ev_.ip = ev->ip; - ev_.type = EV_WRITE; - ev_.param = ev->param; - ev_.data = ev_data; - ev_.callback = write_callback; - event_add(&ev_); +static void response_http_400_page(event_t *ev) +{ + send_response(ev, "400 Bad Request", NULL); } static void response_http_404_page(event_t *ev) { - static char *http_error_404_page = - "" CRLF - "404 Not Found" CRLF - "" CRLF - "

404 Not Found

" CRLF - ""; - - char header[BUFFER_UNIT] = { 0 }; - event_data_t* ev_data = NULL; - int len; - event_t ev_ = {0}; - - len = strlen(http_error_404_page); - sprintf(header, response_header_format(), "404 Not Found", reponse_content_type(NULL), len); - ev_data = create_event_data(header, http_error_404_page); + send_response(ev, "404 Not Found", NULL); +} - ev_.fd = ev->fd; - ev_.ip = ev->ip; - ev_.type = EV_WRITE; - ev_.param = ev->param; - ev_.data = ev_data; - ev_.callback = write_callback; - event_add(&ev_); +static void response_http_500_page(event_t *ev) +{ + send_response(ev, "500 Internal Server Error", NULL); } static void response_http_501_page(event_t *ev) { - const char *http_error_501_page = - "" CRLF - "501 Not Implemented" CRLF - "" CRLF - "

501 Not Implemented

" CRLF - ""; + send_response(ev, "501 Not Implemented", NULL); +} +static void send_response(event_t *ev, char *title, char *status) +{ char header[BUFFER_UNIT] = { 0 }; + char body[BUFFER_UNIT] = { 0 }; event_data_t* ev_data = NULL; - int len; event_t ev_ = {0}; - len = strlen(http_error_501_page); - sprintf(header, response_header_format(), "501 Not Implemented", reponse_content_type(NULL), len); - ev_data = create_event_data(header, http_error_501_page); + sprintf(body, response_body_format(), title, title); + sprintf(header, response_header_format(), status ? status : title, reponse_content_type(NULL), strlen(body)); + ev_data = create_event_data(header, body); ev_.fd = ev->fd; ev_.ip = ev->ip; diff --git a/httpd/logger.h b/httpd/logger.h index e035eea..39f4386 100644 --- a/httpd/logger.h +++ b/httpd/logger.h @@ -10,7 +10,7 @@ typedef enum LOG_ERRO } log_level_t; -#define LOG_LEVEL LOG_DEBU +#define LOG_LEVEL LOG_INFO #define SAVE_FILE 1 void log_debug(const char *fmt, ...);