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

Moved store_body to own file

This commit is contained in:
Lammert Bies
2016-12-11 00:20:13 +01:00
parent 2d449ab215
commit 88a7d7e95c
3 changed files with 84 additions and 49 deletions

View File

@@ -117,6 +117,7 @@ LIB_SOURCES = src/libhttp.c \
src/httplib_sslize.c \
src/httplib_start.c \
src/httplib_stop.c \
src/httplib_store_body.c \
src/httplib_substitute_index_file.c \
src/httplib_timer.c \
src/httplib_tls_dtor.c \

83
src/httplib_store_body.c Normal file
View File

@@ -0,0 +1,83 @@
/*
* 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"
/*
* long long mg_store_body( struct mg_connection *conn, const char *path );
*
* The function mg_store_body() stores in incoming body for future processing.
*/
long long mg_store_body( struct mg_connection *conn, const char *path ) {
char buf[MG_BUF_LEN];
long long len = 0;
int ret;
int n;
struct file fi;
if (conn->consumed_content != 0) {
mg_cry(conn, "%s: Contents already consumed", __func__);
return -11;
}
ret = XX_httplib_put_dir(conn, path);
if (ret < 0) {
/* -1 for path too long,
* -2 for path can not be created. */
return ret;
}
if (ret != 1) {
/* Return 0 means, path itself is a directory. */
return 0;
}
if (XX_httplib_fopen(conn, path, "w", &fi) == 0) return -12;
ret = mg_read(conn, buf, sizeof(buf));
while (ret > 0) {
n = (int)fwrite(buf, 1, (size_t)ret, fi.fp);
if (n != ret) {
XX_httplib_fclose(&fi);
XX_httplib_remove_bad_file(conn, path);
return -13;
}
ret = mg_read(conn, buf, sizeof(buf));
}
/* TODO: XX_httplib_fclose should return an error,
* and every caller should check and handle it. */
if (fclose(fi.fp) != 0) {
XX_httplib_remove_bad_file(conn, path);
return -14;
}
return len;
} /* mg_store_body */

View File

@@ -5142,52 +5142,3 @@ void XX_httplib_remove_bad_file( const struct mg_connection *conn, const char *p
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 ) {
char buf[MG_BUF_LEN];
long long len = 0;
int ret;
int n;
struct file fi;
if (conn->consumed_content != 0) {
mg_cry(conn, "%s: Contents already consumed", __func__);
return -11;
}
ret = XX_httplib_put_dir(conn, path);
if (ret < 0) {
/* -1 for path too long,
* -2 for path can not be created. */
return ret;
}
if (ret != 1) {
/* Return 0 means, path itself is a directory. */
return 0;
}
if (XX_httplib_fopen(conn, path, "w", &fi) == 0) return -12;
ret = mg_read(conn, buf, sizeof(buf));
while (ret > 0) {
n = (int)fwrite(buf, 1, (size_t)ret, fi.fp);
if (n != ret) {
XX_httplib_fclose(&fi);
XX_httplib_remove_bad_file(conn, path);
return -13;
}
ret = mg_read(conn, buf, sizeof(buf));
}
/* TODO: XX_httplib_fclose should return an error,
* and every caller should check and handle it. */
if (fclose(fi.fp) != 0) {
XX_httplib_remove_bad_file(conn, path);
return -14;
}
return len;
} /* mg_store_body */