diff --git a/Makefile b/Makefile index 5093e82b..4191bf57 100644 --- a/Makefile +++ b/Makefile @@ -62,6 +62,7 @@ LIB_SOURCES = src/libhttp.c \ src/httplib_get_uri_type.c \ src/httplib_getreq.c \ src/httplib_handle_file_based_request.c \ + src/httplib_handle_form_request.c \ src/httplib_handle_request.c \ src/httplib_initialize_ssl.c \ src/httplib_load_dll.c \ diff --git a/src/handle_form.inl b/src/httplib_handle_form_request.c similarity index 97% rename from src/handle_form.inl rename to src/httplib_handle_form_request.c index 1c3d99fd..17a4dec0 100644 --- a/src/handle_form.inl +++ b/src/httplib_handle_form_request.c @@ -1,4 +1,6 @@ -/* Copyright (c) 2016 the Civetweb developers +/* + * Copyright (c) 2016 Lammert Bies + * Copyright (c) 2016 the Civetweb developers * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,6 +22,11 @@ */ + +#include "libhttp-private.h" + + + static int url_encoded_field_found(const struct mg_connection *conn, const char *key, size_t key_len, @@ -242,7 +249,7 @@ int mg_handle_form_request(struct mg_connection *conn, struct mg_form_data_handl mg_cry(conn, "%s: Cannot write file %s", __func__, path); fclose(fstore.fp); fstore.fp = NULL; - remove_bad_file(conn, path); + XX_httplib_remove_bad_file(conn, path); } file_size += (int64_t)n; @@ -253,7 +260,7 @@ int mg_handle_form_request(struct mg_connection *conn, struct mg_form_data_handl field_stored(conn, path, file_size, fdh); } else { mg_cry(conn, "%s: Error saving file %s", __func__, path); - remove_bad_file(conn, path); + XX_httplib_remove_bad_file(conn, path); } fstore.fp = NULL; } @@ -392,7 +399,7 @@ int mg_handle_form_request(struct mg_connection *conn, struct mg_form_data_handl mg_cry(conn, "%s: Cannot write file %s", __func__, path); fclose(fstore.fp); fstore.fp = NULL; - remove_bad_file(conn, path); + XX_httplib_remove_bad_file(conn, path); } file_size += (int64_t)n; } @@ -433,7 +440,7 @@ int mg_handle_form_request(struct mg_connection *conn, struct mg_form_data_handl field_stored(conn, path, file_size, fdh); } else { mg_cry(conn, "%s: Error saving file %s", __func__, path); - remove_bad_file(conn, path); + XX_httplib_remove_bad_file(conn, path); } fstore.fp = NULL; } @@ -535,7 +542,7 @@ int mg_handle_form_request(struct mg_connection *conn, struct mg_form_data_handl return -1; } - parse_http_headers(&hbuf, &part_header); + XX_httplib_parse_http_headers(&hbuf, &part_header); if ((hend + 2) != hbuf) { /* Malformed request */ return -1; @@ -630,7 +637,7 @@ int mg_handle_form_request(struct mg_connection *conn, struct mg_form_data_handl mg_cry(conn, "%s: Cannot write file %s", __func__, path); fclose(fstore.fp); fstore.fp = NULL; - remove_bad_file(conn, path); + XX_httplib_remove_bad_file(conn, path); } file_size += (int64_t)n; } @@ -678,7 +685,7 @@ int mg_handle_form_request(struct mg_connection *conn, struct mg_form_data_handl mg_cry(conn, "%s: Cannot write file %s", __func__, path); fclose(fstore.fp); fstore.fp = NULL; - remove_bad_file(conn, path); + XX_httplib_remove_bad_file(conn, path); } file_size += (int64_t)n; } @@ -693,7 +700,7 @@ int mg_handle_form_request(struct mg_connection *conn, struct mg_form_data_handl field_stored(conn, path, file_size, fdh); } else { mg_cry(conn, "%s: Error saving file %s", __func__, path); - remove_bad_file(conn, path); + XX_httplib_remove_bad_file(conn, path); } fstore.fp = NULL; } diff --git a/src/libhttp-private.h b/src/libhttp-private.h index b42ee92e..aa81f28c 100644 --- a/src/libhttp-private.h +++ b/src/libhttp-private.h @@ -881,6 +881,7 @@ int XX_httplib_match_prefix(const char *pattern, size_t pattern_len, const cha void XX_httplib_mkcol( struct mg_connection *conn, const char *path ); int XX_httplib_must_hide_file( struct mg_connection *conn, const char *path ); const char * XX_httplib_next_option( const char *list, struct vec *val, struct vec *eq_val ); +int XX_httplib_parse_http_headers( char **buf, struct mg_request_info *ri ); int XX_httplib_parse_http_message( char *buf, int len, struct mg_request_info *ri ); int XX_httplib_parse_net( const char *spec, uint32_t *net, uint32_t *mask ); void XX_httplib_process_new_connection( struct mg_connection *conn ); @@ -890,6 +891,7 @@ int XX_httplib_read_request( FILE *fp, struct mg_connection *conn, char *buf, void XX_httplib_read_websocket( struct mg_connection *conn, mg_websocket_data_handler ws_data_handler, void *callback_data ); void XX_httplib_redirect_to_https_port( struct mg_connection *conn, int ssl_index ); int XX_httplib_refresh_trust( struct mg_connection *conn ); +void XX_httplib_remove_bad_file( const struct mg_connection *conn, const char *path ); void XX_httplib_remove_double_dots_and_double_slashes( char *s ); void XX_httplib_reset_per_request_attributes( struct mg_connection *conn ); void XX_httplib_send_authorization_request( struct mg_connection *conn ); diff --git a/src/libhttp.c b/src/libhttp.c index 02e493d2..672eff7f 100644 --- a/src/libhttp.c +++ b/src/libhttp.c @@ -5122,12 +5122,13 @@ static int put_dir(struct mg_connection *conn, const char *path) { } -static void remove_bad_file(const struct mg_connection *conn, const char *path) { +void XX_httplib_remove_bad_file( const struct mg_connection *conn, const char *path ) { int r = mg_remove(conn, path); if (r != 0) mg_cry(conn, "%s: Cannot remove invalid file %s", __func__, path); -} + +} /* XX_httplib_remove_bad_file */ long long mg_store_body( struct mg_connection *conn, const char *path ) { @@ -5161,7 +5162,7 @@ long long mg_store_body( struct mg_connection *conn, const char *path ) { n = (int)fwrite(buf, 1, (size_t)ret, fi.fp); if (n != ret) { XX_httplib_fclose(&fi); - remove_bad_file(conn, path); + XX_httplib_remove_bad_file(conn, path); return -13; } ret = mg_read(conn, buf, sizeof(buf)); @@ -5170,7 +5171,7 @@ long long mg_store_body( struct mg_connection *conn, const char *path ) { /* TODO: XX_httplib_fclose should return an error, * and every caller should check and handle it. */ if (fclose(fi.fp) != 0) { - remove_bad_file(conn, path); + XX_httplib_remove_bad_file(conn, path); return -14; } @@ -5182,7 +5183,7 @@ long long mg_store_body( struct mg_connection *conn, const char *path ) { * to the point where parsing stopped. * All parameters must be valid pointers (not NULL). * Return <0 on error. */ -static int parse_http_headers(char **buf, struct mg_request_info *ri) { +int XX_httplib_parse_http_headers( char **buf, struct mg_request_info *ri ) { int i; @@ -5234,7 +5235,8 @@ static int parse_http_headers(char **buf, struct mg_request_info *ri) { } } return ri->num_headers; -} + +} /* XX_httplib_parse_http_headers */ static int is_valid_http_method(const char *method) { @@ -5313,7 +5315,7 @@ int XX_httplib_parse_http_message( char *buf, int len, struct mg_request_info *r return -1; } if (is_request) ri->http_version += 5; - if (parse_http_headers(&buf, ri) < 0) { + if (XX_httplib_parse_http_headers(&buf, ri) < 0) { /* Error while parsing headers */ return -1; } @@ -5887,7 +5889,7 @@ void XX_httplib_handle_cgi_request( struct mg_connection *conn, const char *prog } pbuf = buf; buf[headers_len - 1] = '\0'; - parse_http_headers(&pbuf, &ri); + XX_httplib_parse_http_headers(&pbuf, &ri); /* Make up and send the status line */ status_text = "OK"; @@ -7250,7 +7252,3 @@ uint32_t XX_httplib_get_remote_ip( const struct mg_connection *conn ) { return ntohl(*(const uint32_t *)&conn->client.rsa.sin.sin_addr); } /* XX_httplib_get_remote_ip */ - - -/* The mg_upload function is superseeded by mg_handle_form_request. */ -#include "handle_form.inl"