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

Moved fgets to own file

This commit is contained in:
Lammert Bies
2016-12-11 02:40:41 +01:00
parent 6149a24007
commit 274d3f9e02
3 changed files with 58 additions and 29 deletions

View File

@@ -61,6 +61,7 @@ LIB_SOURCES = src/libhttp.c \
src/httplib_dir_scan_callback.c \
src/httplib_download.c \
src/httplib_fclose_on_exec.c \
src/httplib_fgets.c \
src/httplib_forward_body_data.c \
src/httplib_free_context.c \
src/httplib_get_first_ssl_listener_index.c \

57
src/httplib_fgets.c Normal file
View File

@@ -0,0 +1,57 @@
/*
* 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"
const char *XX_httplib_fgets( char *buf, size_t size, struct file *filep, char **p ) {
const char *eof;
size_t len;
const char *memend;
if ( filep == NULL ) return NULL;
if (filep->membuf != NULL && *p != NULL) {
memend = (const char *)&filep->membuf[filep->size];
/* Search for \n from p till the end of stream */
eof = (char *)memchr(*p, '\n', (size_t)(memend - *p));
if (eof != NULL) {
eof += 1; /* Include \n */
} else {
eof = memend; /* Copy remaining data */
}
len = ((size_t)(eof - *p) > (size - 1)) ? (size - 1) : (size_t)(eof - *p);
memcpy(buf, *p, len);
buf[len] = '\0';
*p += len;
return len ? eof : NULL;
} else if (filep->fp != NULL) {
return fgets(buf, (int)size, filep->fp);
} else return NULL;
} /* XX_httplib_fgets */

View File

@@ -3977,32 +3977,3 @@ int XX_httplib_parse_auth_header(struct mg_connection *conn, char *buf, size_t b
return 1;
} /* XX_httplib_parse_auth_header */
const char *XX_httplib_fgets( char *buf, size_t size, struct file *filep, char **p ) {
const char *eof;
size_t len;
const char *memend;
if (!filep) return NULL;
if (filep->membuf != NULL && *p != NULL) {
memend = (const char *)&filep->membuf[filep->size];
/* Search for \n from p till the end of stream */
eof = (char *)memchr(*p, '\n', (size_t)(memend - *p));
if (eof != NULL) {
eof += 1; /* Include \n */
} else {
eof = memend; /* Copy remaining data */
}
len = ((size_t)(eof - *p) > (size - 1)) ? (size - 1) : (size_t)(eof - *p);
memcpy(buf, *p, len);
buf[len] = '\0';
*p += len;
return len ? eof : NULL;
} else if (filep->fp != NULL) {
return fgets(buf, (int)size, filep->fp);
} else return NULL;
} /* XX_httplib_fgets */