(http_lib.c) Cleanups

This commit is contained in:
twinaphex 2015-01-21 00:39:10 +01:00
parent 9e64ff2a86
commit 300e5ebd0d
2 changed files with 24 additions and 26 deletions

View File

@ -84,7 +84,7 @@ static int http_read_line (int fd, char *buffer, int max)
{ {
if (read(fd, buffer, 1) != 1) if (read(fd, buffer, 1) != 1)
{ {
n= -n; n = -n;
break; break;
} }
n++; n++;
@ -211,7 +211,10 @@ static http_retcode http_query(const char *command, const char *url,
ret=ERRCONN; ret=ERRCONN;
else else
{ {
if (pfd) *pfd=s; int n;
if (pfd)
*pfd=s;
/* create header */ /* create header */
if (proxy) if (proxy)
@ -251,15 +254,15 @@ static http_retcode http_query(const char *command, const char *url,
else else
{ {
/* read result & check */ /* read result & check */
int linelen=http_read_line(s,header,MAXBUF-1); n = http_read_line(s,header,MAXBUF-1);
#ifdef VERBOSE #ifdef VERBOSE
fputs(header,stderr); fputs(header,stderr);
putc('\n',stderr); putc('\n',stderr);
#endif #endif
if (linelen<=0) if (n <= 0)
ret=ERRRDHD; ret = ERRRDHD;
else if (sscanf(header, "HTTP/1.%*d %03d", (int*)&ret) != 1) else if (sscanf(header, "HTTP/1.%*d %03d", (int*)&ret) != 1)
ret=ERRPAHD; ret = ERRPAHD;
else if (mode == KEEP_OPEN) else if (mode == KEEP_OPEN)
return ret; return ret;
} }
@ -331,11 +334,8 @@ http_retcode http_get(const char *filename,
char **pdata, int *plength, char *typebuf) char **pdata, int *plength, char *typebuf)
{ {
http_retcode ret; http_retcode ret;
char header[MAXBUF], *pc;
char header[MAXBUF]; int fd, n, length = -1;
char *pc;
int fd;
int n,length=-1;
if (!pdata) if (!pdata)
return ERRNULL; return ERRNULL;
@ -353,7 +353,7 @@ http_retcode http_get(const char *filename,
{ {
while (1) while (1)
{ {
n=http_read_line(fd,header,MAXBUF-1); n = http_read_line(fd,header,MAXBUF-1);
#ifdef VERBOSE #ifdef VERBOSE
fputs(header,stderr); fputs(header,stderr);
putc('\n',stderr); putc('\n',stderr);
@ -381,13 +381,15 @@ http_retcode http_get(const char *filename,
return ERRNOLG; return ERRNOLG;
} }
if (plength) if (plength)
*plength=length; *plength = length;
if (!(*pdata = (char*)malloc(length))) if (!(*pdata = (char*)malloc(length)))
{ {
close(fd); close(fd);
return ERRMEM; return ERRMEM;
} }
n=http_read_buffer(fd, *pdata, length);
n = http_read_buffer(fd, *pdata, length);
close(fd); close(fd);
if (n != length) if (n != length)
ret = ERRRDDT; ret = ERRRDDT;
@ -422,13 +424,9 @@ http_retcode http_get(const char *filename,
**/ **/
http_retcode http_head(const char *filename, int *plength, char *typebuf) http_retcode http_head(const char *filename, int *plength, char *typebuf)
{ {
/* mostly copied from http_get : */
http_retcode ret; http_retcode ret;
char header[MAXBUF], *pc;
char header[MAXBUF]; int fd, n, length = -1;
char *pc;
int fd;
int n,length=-1;
if (plength) if (plength)
*plength=0; *plength=0;
@ -436,6 +434,7 @@ http_retcode http_head(const char *filename, int *plength, char *typebuf)
*typebuf='\0'; *typebuf='\0';
ret=http_query("HEAD", filename, "", KEEP_OPEN, NULL, 0, &fd); ret=http_query("HEAD", filename, "", KEEP_OPEN, NULL, 0, &fd);
if (ret == 200) if (ret == 200)
{ {
while (1) while (1)
@ -445,7 +444,7 @@ http_retcode http_head(const char *filename, int *plength, char *typebuf)
fputs(header, stderr); fputs(header, stderr);
putc('\n', stderr); putc('\n', stderr);
#endif #endif
if (n<=0) if (n <= 0)
{ {
close(fd); close(fd);
return ERRRDHD; return ERRRDHD;
@ -458,17 +457,17 @@ http_retcode http_head(const char *filename, int *plength, char *typebuf)
/* Try to parse some keywords : */ /* Try to parse some keywords : */
/* convert to lower case 'till a : is found or end of string */ /* convert to lower case 'till a : is found or end of string */
for (pc=header; (*pc != ':' && *pc) ; pc++) for (pc=header; (*pc != ':' && *pc) ; pc++)
*pc=tolower(*pc); *pc = tolower(*pc);
sscanf(header, "content-length: %d", &length); sscanf(header, "content-length: %d", &length);
if (typebuf) if (typebuf)
sscanf(header, "content-type: %s", typebuf); sscanf(header, "content-type: %s", typebuf);
} }
if (plength) if (plength)
*plength=length; *plength = length;
close(fd); close(fd);
} }
else if (ret>=0) else if (ret >= 0)
close(fd); close(fd);
return ret; return ret;
} }
@ -559,5 +558,5 @@ http_retcode http_parse_url(char *url, char **pfilename)
fprintf(stderr,"host=(%s), port=%d, filename=(%s)\n", fprintf(stderr,"host=(%s), port=%d, filename=(%s)\n",
http_server, http_port, *pfilename); http_server, http_port, *pfilename);
#endif #endif
return OK0; return 0;
} }

View File

@ -48,7 +48,6 @@ typedef enum
ERR503 = 503, /* Service overloaded */ ERR503 = 503, /* Service overloaded */
/* Succesful results */ /* Succesful results */
OK0 = 0, /* successfully parsed */
OK201 = 201, /* Resource succesfully created */ OK201 = 201, /* Resource succesfully created */
OK200 = 200 /* Resource succesfully read */ OK200 = 200 /* Resource succesfully read */