bool newsession)
{
struct CookieInfo *c;
- char *line = NULL;
FILE *handle = NULL;
if(!inc) {
c->running = FALSE; /* this is not running, this is init */
if(fp) {
-
- line = malloc(MAX_COOKIE_LINE);
- if(!line)
- goto fail;
- while(Curl_get_line(line, MAX_COOKIE_LINE, fp)) {
- char *lineptr = line;
+ struct dynbuf buf;
+ Curl_dyn_init(&buf, MAX_COOKIE_LINE);
+ while(Curl_get_line(&buf, fp)) {
+ char *lineptr = Curl_dyn_ptr(&buf);
bool headerline = FALSE;
- if(checkprefix("Set-Cookie:", line)) {
+ if(checkprefix("Set-Cookie:", lineptr)) {
/* This is a cookie line, get it! */
- lineptr = &line[11];
+ lineptr += 11;
headerline = TRUE;
while(*lineptr && ISBLANK(*lineptr))
lineptr++;
Curl_cookie_add(data, c, headerline, TRUE, lineptr, NULL, NULL, TRUE);
}
- free(line); /* free the line buffer */
+ Curl_dyn_free(&buf); /* free the line buffer */
/*
* Remove expired cookies from the hash. We must make sure to run this
c->running = TRUE; /* now, we're running */
return c;
-
-fail:
- free(line);
- /*
- * Only clean up if we allocated it here, as the original could still be in
- * use by a share handle.
- */
- if(!inc)
- Curl_cookie_cleanup(c);
- if(handle)
- fclose(handle);
- return NULL; /* out of memory */
}
/*
#include "memdebug.h"
/*
- * Curl_get_line() makes sure to only return complete whole lines that fit in
- * 'len' bytes and end with a newline.
+ * Curl_get_line() makes sure to only return complete whole lines that end
+ * newlines.
*/
-char *Curl_get_line(char *buf, int len, FILE *input)
+int Curl_get_line(struct dynbuf *buf, FILE *input)
{
- bool partial = FALSE;
+ CURLcode result;
+ char buffer[128];
+ Curl_dyn_reset(buf);
while(1) {
- char *b = fgets(buf, len, input);
+ char *b = fgets(buffer, sizeof(buffer), input);
if(b) {
size_t rlen = strlen(b);
if(!rlen)
break;
- if(b[rlen-1] == '\n') {
- /* b is \n terminated */
- if(partial) {
- partial = FALSE;
- continue;
- }
- return b;
- }
- else if(feof(input)) {
- if(partial)
- /* Line is already too large to return, ignore rest */
- break;
+ result = Curl_dyn_addn(buf, b, rlen);
+ if(result)
+ /* too long line or out of memory */
+ return 0; /* error */
- if(rlen + 1 < (size_t) len) {
- /* b is EOF terminated, insert missing \n */
- b[rlen] = '\n';
- b[rlen + 1] = '\0';
- return b;
- }
- else
- /* Maximum buffersize reached + EOF
- * This line is impossible to add a \n to so we'll ignore it
- */
- break;
+ else if(b[rlen-1] == '\n')
+ /* end of the line */
+ return 1; /* all good */
+
+ else if(feof(input)) {
+ /* append a newline */
+ result = Curl_dyn_addn(buf, "\n", 1);
+ if(result)
+ /* too long line or out of memory */
+ return 0; /* error */
+ return 1; /* all good */
}
- else
- /* Maximum buffersize reached */
- partial = TRUE;
}
else
break;
}
- return NULL;
+ return 0;
}
#endif /* if not disabled */
"LINE1\n"
C4096 "SOME EXTRA TEXT",
- /* First and third line should be read */
+ /* Only first should be read */
"LINE1\n"
C4096 "SOME EXTRA TEXT\n"
"LINE3\n",
UNITTEST_START
size_t i;
+ int rc = 0;
for(i = 0; i < NUMTESTS; i++) {
FILE *fp;
- char buf[4096];
+ struct dynbuf buf;
int len = 4096;
char *line;
+ Curl_dyn_init(&buf, len);
fp = fopen(arg, "wb");
abort_unless(fp != NULL, "Cannot open testfile");
fprintf(stderr, "Test %zd...", i);
switch(i) {
case 0:
- line = Curl_get_line(buf, len, fp);
+ rc = Curl_get_line(&buf, fp);
+ line = Curl_dyn_ptr(&buf);
fail_unless(line && !strcmp("LINE1\n", line),
- "First line failed (1)");
- line = Curl_get_line(buf, len, fp);
+ "First line failed (1)");
+ rc = Curl_get_line(&buf, fp);
+ line = Curl_dyn_ptr(&buf);
fail_unless(line && !strcmp("LINE2 NEWLINE\n", line),
- "Second line failed (1)");
- line = Curl_get_line(buf, len, fp);
- abort_unless(line == NULL, "Missed EOF (1)");
+ "Second line failed (1)");
+ rc = Curl_get_line(&buf, fp);
+ abort_unless(!Curl_dyn_len(&buf), "Missed EOF (1)");
break;
case 1:
- line = Curl_get_line(buf, len, fp);
+ rc = Curl_get_line(&buf, fp);
+ line = Curl_dyn_ptr(&buf);
fail_unless(line && !strcmp("LINE1\n", line),
- "First line failed (2)");
- line = Curl_get_line(buf, len, fp);
+ "First line failed (2)");
+ rc = Curl_get_line(&buf, fp);
+ line = Curl_dyn_ptr(&buf);
fail_unless(line && !strcmp("LINE2 NONEWLINE\n", line),
- "Second line failed (2)");
- line = Curl_get_line(buf, len, fp);
- abort_unless(line == NULL, "Missed EOF (2)");
+ "Second line failed (2)");
+ rc = Curl_get_line(&buf, fp);
+ abort_unless(!Curl_dyn_len(&buf), "Missed EOF (2)");
break;
case 2:
- line = Curl_get_line(buf, len, fp);
+ rc = Curl_get_line(&buf, fp);
+ line = Curl_dyn_ptr(&buf);
fail_unless(line && !strcmp("LINE1\n", line),
- "First line failed (3)");
- line = Curl_get_line(buf, len, fp);
- fail_unless(line == NULL,
- "Did not detect max read on EOF (3)");
+ "First line failed (3)");
+ rc = Curl_get_line(&buf, fp);
+ fail_unless(!Curl_dyn_len(&buf),
+ "Did not detect max read on EOF (3)");
break;
case 3:
- line = Curl_get_line(buf, len, fp);
+ rc = Curl_get_line(&buf, fp);
+ line = Curl_dyn_ptr(&buf);
fail_unless(line && !strcmp("LINE1\n", line),
- "First line failed (4)");
- line = Curl_get_line(buf, len, fp);
- fail_unless(line == NULL,
- "Did not ignore partial on EOF (4)");
+ "First line failed (4)");
+ rc = Curl_get_line(&buf, fp);
+ fail_unless(!Curl_dyn_len(&buf),
+ "Did not ignore partial on EOF (4)");
break;
case 4:
- line = Curl_get_line(buf, len, fp);
+ rc = Curl_get_line(&buf, fp);
+ line = Curl_dyn_ptr(&buf);
fail_unless(line && !strcmp("LINE1\n", line),
- "First line failed (5)");
- line = Curl_get_line(buf, len, fp);
- fail_unless(line && !strcmp("LINE3\n", line),
- "Third line failed (5)");
- line = Curl_get_line(buf, len, fp);
- abort_unless(line == NULL, "Missed EOF (5)");
+ "First line failed (5)");
+ rc = Curl_get_line(&buf, fp);
+ fail_unless(!Curl_dyn_len(&buf),
+ "Did not bail out on too long line");
break;
case 5:
- line = Curl_get_line(buf, len, fp);
+ rc = Curl_get_line(&buf, fp);
+ line = Curl_dyn_ptr(&buf);
fail_unless(line && !strcmp("LINE1\x1aTEST\n", line),
- "Missed/Misinterpreted ^Z (6)");
- line = Curl_get_line(buf, len, fp);
- abort_unless(line == NULL, "Missed EOF (6)");
+ "Missed/Misinterpreted ^Z (6)");
+ rc = Curl_get_line(&buf, fp);
+ abort_unless(!Curl_dyn_len(&buf), "Missed EOF (6)");
break;
default:
abort_unless(1, "Unknown case");
break;
}
+ Curl_dyn_free(&buf);
fclose(fp);
fprintf(stderr, "OK\n");
}
+ return rc;
UNITTEST_STOP
#ifdef __GNUC__