diff --git a/Makefile b/Makefile index 9d240216..bccf13f0 100644 --- a/Makefile +++ b/Makefile @@ -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 \ diff --git a/src/httplib_fgets.c b/src/httplib_fgets.c new file mode 100644 index 00000000..33e2ece9 --- /dev/null +++ b/src/httplib_fgets.c @@ -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 */ diff --git a/src/libhttp.c b/src/libhttp.c index ea79ff9c..72a1014f 100644 --- a/src/libhttp.c +++ b/src/libhttp.c @@ -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 */