diff mbox series

[1/2] mongoose: split upload_handler

Message ID 1520243058-10526-1-git-send-email-sbabic@denx.de
State Changes Requested
Headers show
Series [1/2] mongoose: split upload_handler | expand

Commit Message

Stefano Babic March 5, 2018, 9:44 a.m. UTC
To make code more readable, split upload_handler into
two function for v1 and v2 API.

Signed-off-by: Stefano Babic <sbabic@denx.de>
CC: Sami Hartikainen <sami.hartikainen@teleste.com>
---
 mongoose/mongoose_interface.c | 31 ++++++++++++++++++-------------
 1 file changed, 18 insertions(+), 13 deletions(-)

Comments

Hartikainen, Sami March 5, 2018, 10:38 a.m. UTC | #1
Hi Stefano,

Please find two comments below.

> From: Stefano Babic [mailto:sbabic@denx.de]
> Sent: maanantai 5. maaliskuuta 2018 11.44
> To: swupdate@googlegroups.com
> Cc: Stefano Babic <sbabic@denx.de>; Hartikainen, Sami
> <Sami.Hartikainen@teleste.com>
> Subject: [PATCH 1/2] mongoose: split upload_handler
> 
> To make code more readable, split upload_handler into two function for v1
> and v2 API.
> 
> Signed-off-by: Stefano Babic <sbabic@denx.de>
> CC: Sami Hartikainen <sami.hartikainen@teleste.com>
> ---
>  mongoose/mongoose_interface.c | 31 ++++++++++++++++++-------------
>  1 file changed, 18 insertions(+), 13 deletions(-)
> 
> diff --git a/mongoose/mongoose_interface.c
> b/mongoose/mongoose_interface.c index 60914d3..423a2dc 100644
> --- a/mongoose/mongoose_interface.c
> +++ b/mongoose/mongoose_interface.c
> @@ -112,20 +112,16 @@ static size_t snescape(char *dst, size_t n, const char
> *src)  }  #endif
> 
> -static void upload_handler(struct mg_connection *nc, int ev, void *p) -{
> -	struct mg_http_multipart_part *mp;
> -	struct file_upload_state *fus;
>  #if defined(CONFIG_MONGOOSE_WEB_API_V1)
> +static void upload_handler_v1(struct mg_connection *nc, int ev, void
> +*p) {
>  	struct mg_str *filename, *data;
>  	struct http_message *hm;
>  	size_t length;
>  	char buf[16];
>  	int fd;
> -#endif
> 
>  	switch (ev) {
> -#if defined(CONFIG_MONGOOSE_WEB_API_V1)
>  	case MG_EV_HTTP_REQUEST:
>  		hm = (struct http_message *) p;
> 
> @@ -159,9 +155,20 @@ static void upload_handler(struct mg_connection
> *nc, int ev, void *p)
>  		mg_send(nc, "\r\n", 2);
>  		mg_printf(nc, "Ok, %.*s - %d bytes.\r\n", (int) filename->len,
> filename->p, (int) length);
>  		nc->flags |= MG_F_SEND_AND_CLOSE;
> -
>  		break;
> +	default:
> +		upload_handler(nc, ev, p);
> +		break;
> +	}
> +}
>  #endif

The upload_handler() function is called before being defined. (Although patch 2/2 fixes this.)

> +static void upload_handler(struct mg_connection *nc, int ev, void *p) {
> +	struct mg_http_multipart_part *mp;
> +	struct file_upload_state *fus;
> +
> +	switch (ev) {
>  	case MG_EV_HTTP_PART_BEGIN:
>  		mp = (struct mg_http_multipart_part *) p;
> 
> @@ -610,18 +617,16 @@ int start_mongoose(const char *cfgfname, int argc,
> char *argv[])
>  		exit(EXIT_FAILURE);
>  	}
> 
> +	mg_set_protocol_http_websocket(nc);
> +	mg_register_http_endpoint(nc, "/upload", MG_CB(upload_handler,
> NULL));

As Stefan pointed out earlier, the "/upload" is V2 handler and should be within V2 ifdef's.

>  #if defined(CONFIG_MONGOOSE_WEB_API_V1)
> -	mg_register_http_endpoint(nc, "/handle_post_request",
> MG_CB(upload_handler, NULL));
> +	mg_register_http_endpoint(nc, "/handle_post_request",
> +MG_CB(upload_handler_v1, NULL));
>  	mg_register_http_endpoint(nc, "/getstatus.json",
> MG_CB(recovery_status, NULL));
>  	mg_register_http_endpoint(nc, "/rebootTarget",
> MG_CB(reboot_target, NULL));
>  	mg_register_http_endpoint(nc, "/postUpdateCommand",
> MG_CB(post_update_cmd, NULL)); -#elif
> defined(CONFIG_MONGOOSE_WEB_API_V2)
> -	mg_register_http_endpoint(nc, "/restart", restart_handler);
>  #endif
> -	mg_register_http_endpoint(nc, "/upload", MG_CB(upload_handler,
> NULL));
> -	mg_set_protocol_http_websocket(nc);
> -
>  #if defined(CONFIG_MONGOOSE_WEB_API_V2)
> +	mg_register_http_endpoint(nc, "/restart", restart_handler);
>  	mg_start_thread(broadcast_message_thread, &mgr);
>  	mg_start_thread(broadcast_progress_thread, &mgr);  #endif
> --
> 2.7.4

Br, Sami
Stefano Babic March 5, 2018, 11:05 a.m. UTC | #2
On 05/03/2018 11:38, Sami.Hartikainen@teleste.com wrote:
> Hi Stefano,
> 
> Please find two comments below.
> 
>> From: Stefano Babic [mailto:sbabic@denx.de]
>> Sent: maanantai 5. maaliskuuta 2018 11.44
>> To: swupdate@googlegroups.com
>> Cc: Stefano Babic <sbabic@denx.de>; Hartikainen, Sami
>> <Sami.Hartikainen@teleste.com>
>> Subject: [PATCH 1/2] mongoose: split upload_handler
>>
>> To make code more readable, split upload_handler into two function for v1
>> and v2 API.
>>
>> Signed-off-by: Stefano Babic <sbabic@denx.de>
>> CC: Sami Hartikainen <sami.hartikainen@teleste.com>
>> ---
>>  mongoose/mongoose_interface.c | 31 ++++++++++++++++++-------------
>>  1 file changed, 18 insertions(+), 13 deletions(-)
>>
>> diff --git a/mongoose/mongoose_interface.c
>> b/mongoose/mongoose_interface.c index 60914d3..423a2dc 100644
>> --- a/mongoose/mongoose_interface.c
>> +++ b/mongoose/mongoose_interface.c
>> @@ -112,20 +112,16 @@ static size_t snescape(char *dst, size_t n, const char
>> *src)  }  #endif
>>
>> -static void upload_handler(struct mg_connection *nc, int ev, void *p) -{
>> -	struct mg_http_multipart_part *mp;
>> -	struct file_upload_state *fus;
>>  #if defined(CONFIG_MONGOOSE_WEB_API_V1)
>> +static void upload_handler_v1(struct mg_connection *nc, int ev, void
>> +*p) {
>>  	struct mg_str *filename, *data;
>>  	struct http_message *hm;
>>  	size_t length;
>>  	char buf[16];
>>  	int fd;
>> -#endif
>>
>>  	switch (ev) {
>> -#if defined(CONFIG_MONGOOSE_WEB_API_V1)
>>  	case MG_EV_HTTP_REQUEST:
>>  		hm = (struct http_message *) p;
>>
>> @@ -159,9 +155,20 @@ static void upload_handler(struct mg_connection
>> *nc, int ev, void *p)
>>  		mg_send(nc, "\r\n", 2);
>>  		mg_printf(nc, "Ok, %.*s - %d bytes.\r\n", (int) filename->len,
>> filename->p, (int) length);
>>  		nc->flags |= MG_F_SEND_AND_CLOSE;
>> -
>>  		break;
>> +	default:
>> +		upload_handler(nc, ev, p);
>> +		break;
>> +	}
>> +}
>>  #endif
> 
> The upload_handler() function is called before being defined. (Although patch 2/2 fixes this.)

Yes, I saw the warning and I fixed it, but it belongs to this patch. I
move it.

> 
>> +static void upload_handler(struct mg_connection *nc, int ev, void *p) {
>> +	struct mg_http_multipart_part *mp;
>> +	struct file_upload_state *fus;
>> +
>> +	switch (ev) {
>>  	case MG_EV_HTTP_PART_BEGIN:
>>  		mp = (struct mg_http_multipart_part *) p;
>>
>> @@ -610,18 +617,16 @@ int start_mongoose(const char *cfgfname, int argc,
>> char *argv[])
>>  		exit(EXIT_FAILURE);
>>  	}
>>
>> +	mg_set_protocol_http_websocket(nc);
>> +	mg_register_http_endpoint(nc, "/upload", MG_CB(upload_handler,
>> NULL));
> 
> As Stefan pointed out earlier, the "/upload" is V2 handler and should be within V2 ifdef's.

Right, I fix this in 2/2. This patch just splits the functions without
changing behavior.

> 
>>  #if defined(CONFIG_MONGOOSE_WEB_API_V1)
>> -	mg_register_http_endpoint(nc, "/handle_post_request",
>> MG_CB(upload_handler, NULL));
>> +	mg_register_http_endpoint(nc, "/handle_post_request",
>> +MG_CB(upload_handler_v1, NULL));
>>  	mg_register_http_endpoint(nc, "/getstatus.json",
>> MG_CB(recovery_status, NULL));
>>  	mg_register_http_endpoint(nc, "/rebootTarget",
>> MG_CB(reboot_target, NULL));
>>  	mg_register_http_endpoint(nc, "/postUpdateCommand",
>> MG_CB(post_update_cmd, NULL)); -#elif
>> defined(CONFIG_MONGOOSE_WEB_API_V2)
>> -	mg_register_http_endpoint(nc, "/restart", restart_handler);
>>  #endif
>> -	mg_register_http_endpoint(nc, "/upload", MG_CB(upload_handler,
>> NULL));
>> -	mg_set_protocol_http_websocket(nc);
>> -
>>  #if defined(CONFIG_MONGOOSE_WEB_API_V2)
>> +	mg_register_http_endpoint(nc, "/restart", restart_handler);
>>  	mg_start_thread(broadcast_message_thread, &mgr);
>>  	mg_start_thread(broadcast_progress_thread, &mgr);  #endif
>> --
>> 2.7.4
> 

Best regards,
Stefano Babic
diff mbox series

Patch

diff --git a/mongoose/mongoose_interface.c b/mongoose/mongoose_interface.c
index 60914d3..423a2dc 100644
--- a/mongoose/mongoose_interface.c
+++ b/mongoose/mongoose_interface.c
@@ -112,20 +112,16 @@  static size_t snescape(char *dst, size_t n, const char *src)
 }
 #endif
 
-static void upload_handler(struct mg_connection *nc, int ev, void *p)
-{
-	struct mg_http_multipart_part *mp;
-	struct file_upload_state *fus;
 #if defined(CONFIG_MONGOOSE_WEB_API_V1)
+static void upload_handler_v1(struct mg_connection *nc, int ev, void *p)
+{
 	struct mg_str *filename, *data;
 	struct http_message *hm;
 	size_t length;
 	char buf[16];
 	int fd;
-#endif
 
 	switch (ev) {
-#if defined(CONFIG_MONGOOSE_WEB_API_V1)
 	case MG_EV_HTTP_REQUEST:
 		hm = (struct http_message *) p;
 
@@ -159,9 +155,20 @@  static void upload_handler(struct mg_connection *nc, int ev, void *p)
 		mg_send(nc, "\r\n", 2);
 		mg_printf(nc, "Ok, %.*s - %d bytes.\r\n", (int) filename->len, filename->p, (int) length);
 		nc->flags |= MG_F_SEND_AND_CLOSE;
-
 		break;
+	default:
+		upload_handler(nc, ev, p);
+		break;
+	}
+}
 #endif
+
+static void upload_handler(struct mg_connection *nc, int ev, void *p)
+{
+	struct mg_http_multipart_part *mp;
+	struct file_upload_state *fus;
+
+	switch (ev) {
 	case MG_EV_HTTP_PART_BEGIN:
 		mp = (struct mg_http_multipart_part *) p;
 
@@ -610,18 +617,16 @@  int start_mongoose(const char *cfgfname, int argc, char *argv[])
 		exit(EXIT_FAILURE);
 	}
 
+	mg_set_protocol_http_websocket(nc);
+	mg_register_http_endpoint(nc, "/upload", MG_CB(upload_handler, NULL));
 #if defined(CONFIG_MONGOOSE_WEB_API_V1)
-	mg_register_http_endpoint(nc, "/handle_post_request", MG_CB(upload_handler, NULL));
+	mg_register_http_endpoint(nc, "/handle_post_request", MG_CB(upload_handler_v1, NULL));
 	mg_register_http_endpoint(nc, "/getstatus.json", MG_CB(recovery_status, NULL));
 	mg_register_http_endpoint(nc, "/rebootTarget", MG_CB(reboot_target, NULL));
 	mg_register_http_endpoint(nc, "/postUpdateCommand", MG_CB(post_update_cmd, NULL));
-#elif defined(CONFIG_MONGOOSE_WEB_API_V2)
-	mg_register_http_endpoint(nc, "/restart", restart_handler);
 #endif
-	mg_register_http_endpoint(nc, "/upload", MG_CB(upload_handler, NULL));
-	mg_set_protocol_http_websocket(nc);
-
 #if defined(CONFIG_MONGOOSE_WEB_API_V2)
+	mg_register_http_endpoint(nc, "/restart", restart_handler);
 	mg_start_thread(broadcast_message_thread, &mgr);
 	mg_start_thread(broadcast_progress_thread, &mgr);
 #endif