1
0
mirror of https://github.com/lammertb/libhttp.git synced 2026-01-27 08:02:47 +03:00

Moved mg_download to own file

This commit is contained in:
Lammert Bies
2016-11-16 17:10:35 +01:00
parent b52e7c30f1
commit 5f33c8d486
4 changed files with 79 additions and 40 deletions

View File

@@ -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 \

73
src/httplib_download.c Normal file
View File

@@ -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 */

View File

@@ -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

View File

@@ -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;
}