(http_intf) Cleanups

This commit is contained in:
twinaphex 2015-01-19 03:51:56 +01:00
parent a9a2c7003b
commit 65cd49c950
2 changed files with 74 additions and 57 deletions

View File

@ -54,17 +54,21 @@ int http_intf_command(unsigned mode, char *url)
{
/* *** PUT *** */
case HTTP_INTF_PUT:
RARCH_LOG("reading stdin...\n");
/* read stdin into memory */
RARCH_LOG("reading stdin...\n");
blocksize = 16384;
lg = 0;
if (!(data = (char*)malloc(blocksize)))
return 3;
while (1)
{
r=read(0, data + lg, blocksize - lg);
if (r<=0)
break;
lg+=r;
if ((3 * lg / 2) > blocksize)

View File

@ -78,6 +78,7 @@ static int http_read_line (int fd, char *buffer, int max)
{
/* not efficient on long lines (multiple unbuffered 1 char reads) */
int n = 0;
while (n<max)
{
if (read(fd, buffer, 1) != 1)
@ -112,6 +113,7 @@ static int http_read_line (int fd, char *buffer, int max)
static int http_read_buffer (int fd, char *buffer, int length)
{
int n,r;
for (n = 0; n < length; n += r)
{
r = read(fd, buffer, length-n);
@ -160,7 +162,8 @@ static http_retcode http_query(const char *command, const char *url,
length - size of data
pfd - pointer to variable where to set file descriptor value
*/
static http_retcode http_query(const char *command, const char *url, const char *additional_header, querymode mode,
static http_retcode http_query(const char *command, const char *url,
const char *additional_header, querymode mode,
const char *data, int length, int *pfd)
{
int s;
@ -172,7 +175,8 @@ static http_retcode http_query(const char *command, const char *url, const char
int proxy = (http_proxy_server != NULL && http_proxy_port != 0);
int port = proxy ? http_proxy_port : http_port ;
if (pfd) *pfd=-1;
if (pfd)
*pfd=-1;
/* get host info by name :*/
if ((hp = gethostbyname( proxy ? http_proxy_server
@ -182,6 +186,7 @@ static http_retcode http_query(const char *command, const char *url, const char
{
memset((char *) &server,0, sizeof(server));
memmove((char *) &server.sin_addr, hp->h_addr, hp->h_length);
server.sin_family = hp->h_addrtype;
server.sin_port = (unsigned short) htons( port );
}
@ -276,19 +281,16 @@ static http_retcode http_query(const char *command, const char *url, const char
was already existing.
type - type of the data, if NULL default type is used.
*/
http_retcode http_put(const char *filename, const char *data, int length, int overwrite, const char *type)
http_retcode http_put(const char *filename, const char *data,
int length, int overwrite, const char *type)
{
char header[MAXBUF];
if (type)
sprintf(header, "Content-length: %d\015\012Content-type: %.64s\015\012%s",
length,
type ,
overwrite ? "Control: overwrite=1\015\012" : ""
);
length, type, overwrite ? "Control: overwrite=1\015\012" : "");
else
sprintf(header, "Content-length: %d\015\012%s",length,
overwrite ? "Control: overwrite=1\015\012" : ""
);
overwrite ? "Control: overwrite=1\015\012" : "");
return http_query("PUT", filename, header, CLOSE, data, length, NULL);
}
@ -317,7 +319,8 @@ http_retcode http_put(const char *filename, const char *data, int length, int ov
If NULL, the type is not returned.
*/
http_retcode http_get(const char *filename, char **pdata, int *plength, char *typebuf)
http_retcode http_get(const char *filename,
char **pdata, int *plength, char *typebuf)
{
http_retcode ret;
@ -337,6 +340,7 @@ http_retcode http_get(const char *filename, char **pdata, int *plength, char *ty
*typebuf = '\0';
ret = http_query("GET", filename, "", KEEP_OPEN, NULL, 0, &fd);
if (ret == 200)
{
while (1)
@ -362,6 +366,7 @@ http_retcode http_get(const char *filename, char **pdata, int *plength, char *ty
if (typebuf)
sscanf(header,"content-type: %s",typebuf);
}
if (length<=0)
{
close(fd);
@ -381,6 +386,7 @@ http_retcode http_get(const char *filename, char **pdata, int *plength, char *ty
}
else if (ret >= 0)
close(fd);
return ret;
}
@ -434,14 +440,17 @@ http_retcode http_head(const char *filename, int *plength, char *typebuf)
close(fd);
return ERRRDHD;
}
/* empty line ? (=> end of header) */
/* Empty line ? (=> end of header) */
if (n > 0 && (*header) == '\0')
break;
/* try to parse some keywords : */
/* Try to parse some keywords : */
/* convert to lower case 'till a : is found or end of string */
for (pc=header; (*pc != ':' && *pc) ; pc++)
*pc=tolower(*pc);
sscanf(header, "content-length: %d", &length);
if (typebuf)
sscanf(header, "content-type: %s", typebuf);
}
@ -490,6 +499,7 @@ http_retcode http_parse_url(char *url, char **pfilename)
char *pc, c;
http_port = 80;
if (http_server)
{
free(http_server);
@ -509,9 +519,12 @@ http_retcode http_parse_url(char *url, char **pfilename)
return ERRURLH;
}
url+=7;
for (pc = url, c = *pc; (c && c != ':' && c != '/');)
c=*pc++;
*(pc-1) = 0;
if (c == ':')
{
if (sscanf(pc, "%d", &http_port) != 1)