From 5f33c8d486fa160547f5aadce353a44fac620bc7 Mon Sep 17 00:00:00 2001 From: Lammert Bies Date: Wed, 16 Nov 2016 17:10:35 +0100 Subject: [PATCH] Moved mg_download to own file --- Makefile | 1 + src/httplib_download.c | 73 ++++++++++++++++++++++++++++++++++++++++++ src/libhttp-private.h | 2 ++ src/libhttp.c | 43 ++----------------------- 4 files changed, 79 insertions(+), 40 deletions(-) create mode 100644 src/httplib_download.c diff --git a/Makefile b/Makefile index 88c4c73e..5a286b19 100644 --- a/Makefile +++ b/Makefile @@ -46,6 +46,7 @@ LIB_SOURCES = src/libhttp.c \ src/httplib_check_feature.c \ src/httplib_connect_websocket_client.c \ src/httplib_consume_socket.c \ + src/httplib_download.c \ src/httplib_free_context.c \ src/httplib_get_response_code_text.c \ src/httplib_get_system_name.c \ diff --git a/src/httplib_download.c b/src/httplib_download.c new file mode 100644 index 00000000..fe1b1af3 --- /dev/null +++ b/src/httplib_download.c @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2016 Lammert Bies + * Copyright (c) 2013-2016 the Civetweb developers + * Copyright (c) 2004-2013 Sergey Lyubka + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + + + +#include "libhttp-private.h" + + + +/* + * struct mg_connection *mg_download(); + * + * The function mg_download() is used to download a file from a remote location + * and returns a pointer to the connection on success, or NULL on error. + */ + +struct mg_connection * mg_download( const char *host, int port, int use_ssl, char *ebuf, size_t ebuf_len, const char *fmt, ... ) { + + struct mg_connection *conn; + va_list ap; + int i; + int reqerr; + + va_start(ap, fmt); + ebuf[0] = '\0'; + + /* open a connection */ + conn = mg_connect_client(host, port, use_ssl, ebuf, ebuf_len); + + if (conn != NULL) { + i = XX_httplib_vprintf(conn, fmt, ap); + if (i <= 0) { + XX_httplib_snprintf(conn, NULL, ebuf, ebuf_len, "%s", "Error sending request"); + } else { + XX_httplib_getreq(conn, ebuf, ebuf_len, &reqerr); + + /* TODO: 1) uri is deprecated; + * 2) here, ri.uri is the http response code */ + conn->request_info.uri = conn->request_info.request_uri; + } + } + + /* if an error occured, close the connection */ + if (ebuf[0] != '\0' && conn != NULL) { + mg_close_connection(conn); + conn = NULL; + } + + va_end(ap); + return conn; + +} /* mg_download */ diff --git a/src/libhttp-private.h b/src/libhttp-private.h index 4f1a8d97..6117a89c 100644 --- a/src/libhttp-private.h +++ b/src/libhttp-private.h @@ -846,6 +846,8 @@ int XX_httplib_sslize( struct mg_connection *conn, SSL_CTX *s, int (*func)(SSL char * XX_httplib_strdup( const char *str ); void XX_httplib_tls_dtor( void *key ); void XX_httplib_uninitialize_ssl( struct mg_context *ctx ); +int XX_httplib_vprintf( struct mg_connection *conn, const char *fmt, va_list ap ); + #ifdef _WIN32 diff --git a/src/libhttp.c b/src/libhttp.c index 7666dcc9..289da6a3 100644 --- a/src/libhttp.c +++ b/src/libhttp.c @@ -3147,7 +3147,7 @@ static int alloc_vprintf(char **out_buf, char *prealloc_buf, size_t prealloc_siz } -static int mg_vprintf(struct mg_connection *conn, const char *fmt, va_list ap) { +int XX_httplib_vprintf( struct mg_connection *conn, const char *fmt, va_list ap ) { char mem[MG_BUF_LEN]; char *buf = NULL; @@ -3159,7 +3159,7 @@ static int mg_vprintf(struct mg_connection *conn, const char *fmt, va_list ap) { if (buf != mem && buf != NULL) XX_httplib_free(buf); return len; -} +} /* XX_httplib_vprintf */ int mg_printf(struct mg_connection *conn, const char *fmt, ...) { @@ -3168,7 +3168,7 @@ int mg_printf(struct mg_connection *conn, const char *fmt, ...) { int result; va_start(ap, fmt); - result = mg_vprintf(conn, fmt, ap); + result = XX_httplib_vprintf(conn, fmt, ap); va_end(ap); return result; @@ -9812,40 +9812,3 @@ int mg_get_response(struct mg_connection *conn, char *ebuf, size_t ebuf_len, int } return -1; } - - -struct mg_connection * mg_download(const char *host, int port, int use_ssl, char *ebuf, size_t ebuf_len, const char *fmt, ...) { - - struct mg_connection *conn; - va_list ap; - int i; - int reqerr; - - va_start(ap, fmt); - ebuf[0] = '\0'; - - /* open a connection */ - conn = mg_connect_client(host, port, use_ssl, ebuf, ebuf_len); - - if (conn != NULL) { - i = mg_vprintf(conn, fmt, ap); - if (i <= 0) { - XX_httplib_snprintf(conn, NULL, ebuf, ebuf_len, "%s", "Error sending request"); - } else { - XX_httplib_getreq(conn, ebuf, ebuf_len, &reqerr); - - /* TODO: 1) uri is deprecated; - * 2) here, ri.uri is the http response code */ - conn->request_info.uri = conn->request_info.request_uri; - } - } - - /* if an error occured, close the connection */ - if (ebuf[0] != '\0' && conn != NULL) { - mg_close_connection(conn); - conn = NULL; - } - - va_end(ap); - return conn; -}