mirror of
https://github.com/cesanta/mongoose.git
synced 2025-01-01 11:07:46 +08:00
Merge pull request #212 from pas2k/master
Consume \n in mg_fgets when reading from membuf
This commit is contained in:
commit
b5d397c74f
15
mongoose.c
15
mongoose.c
@ -2415,14 +2415,21 @@ static int parse_auth_header(struct mg_connection *conn, char *buf,
|
|||||||
static char *mg_fgets(char *buf, size_t size, struct file *filep, char **p) {
|
static char *mg_fgets(char *buf, size_t size, struct file *filep, char **p) {
|
||||||
char *eof;
|
char *eof;
|
||||||
size_t len;
|
size_t len;
|
||||||
|
char *memend;
|
||||||
|
|
||||||
if (filep->membuf != NULL && *p != NULL) {
|
if (filep->membuf != NULL && *p != NULL) {
|
||||||
eof = (char *) memchr(*p, '\n', &filep->membuf[filep->size] - *p);
|
memend = (char *) &filep->membuf[filep->size];
|
||||||
len = (size_t) (eof - *p) > size - 1 ? size - 1 : (size_t) (eof - *p);
|
eof = (char *) memchr(*p, '\n', memend - *p); // Search for \n from p till the end of stream
|
||||||
|
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);
|
memcpy(buf, *p, len);
|
||||||
buf[len] = '\0';
|
buf[len] = '\0';
|
||||||
*p = eof;
|
*p += len;
|
||||||
return eof;
|
return len ? eof : NULL;
|
||||||
} else if (filep->fp != NULL) {
|
} else if (filep->fp != NULL) {
|
||||||
return fgets(buf, size, filep->fp);
|
return fgets(buf, size, filep->fp);
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user