mirror of
https://github.com/libretro/RetroArch
synced 2025-04-09 21:45:45 +00:00
(http_intf) Cleanups
This commit is contained in:
parent
a9a2c7003b
commit
65cd49c950
30
http_intf.c
30
http_intf.c
@ -54,17 +54,21 @@ int http_intf_command(unsigned mode, char *url)
|
|||||||
{
|
{
|
||||||
/* *** PUT *** */
|
/* *** PUT *** */
|
||||||
case HTTP_INTF_PUT:
|
case HTTP_INTF_PUT:
|
||||||
RARCH_LOG("reading stdin...\n");
|
|
||||||
/* read stdin into memory */
|
/* read stdin into memory */
|
||||||
blocksize=16384;
|
RARCH_LOG("reading stdin...\n");
|
||||||
lg=0;
|
blocksize = 16384;
|
||||||
if (!(data=(char*)malloc(blocksize)))
|
lg = 0;
|
||||||
|
|
||||||
|
if (!(data = (char*)malloc(blocksize)))
|
||||||
return 3;
|
return 3;
|
||||||
|
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
r=read(0, data + lg, blocksize - lg);
|
r=read(0, data + lg, blocksize - lg);
|
||||||
|
|
||||||
if (r<=0)
|
if (r<=0)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
lg+=r;
|
lg+=r;
|
||||||
|
|
||||||
if ((3 * lg / 2) > blocksize)
|
if ((3 * lg / 2) > blocksize)
|
||||||
@ -72,29 +76,29 @@ int http_intf_command(unsigned mode, char *url)
|
|||||||
blocksize *= 4;
|
blocksize *= 4;
|
||||||
RARCH_LOG("read to date: %9d bytes, reallocating buffer to %9d\n",
|
RARCH_LOG("read to date: %9d bytes, reallocating buffer to %9d\n",
|
||||||
lg, blocksize);
|
lg, blocksize);
|
||||||
if (!(data=(char*)realloc(data,blocksize)))
|
if (!(data = (char*)realloc(data,blocksize)))
|
||||||
return 4;
|
return 4;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
RARCH_LOG("read %d bytes\n", lg);
|
RARCH_LOG("read %d bytes\n", lg);
|
||||||
ret=http_put(filename,data,lg,0,NULL);
|
ret = http_put(filename,data, lg, 0, NULL);
|
||||||
RARCH_LOG("res=%d\n",ret);
|
RARCH_LOG("res=%d\n", ret);
|
||||||
break;
|
break;
|
||||||
case HTTP_INTF_GET:
|
case HTTP_INTF_GET:
|
||||||
/* *** GET *** */
|
/* *** GET *** */
|
||||||
ret = http_get(filename, &data, &lg, typebuf);
|
ret = http_get(filename, &data, &lg, typebuf);
|
||||||
RARCH_LOG("res=%d,type='%s',lg=%d\n",ret,typebuf,lg);
|
RARCH_LOG("res=%d,type='%s',lg=%d\n", ret, typebuf, lg);
|
||||||
fwrite(data,lg,1,stdout);
|
fwrite(data, lg, 1, stdout);
|
||||||
break;
|
break;
|
||||||
case HTTP_INTF_HEAD:
|
case HTTP_INTF_HEAD:
|
||||||
/* *** HEAD *** */
|
/* *** HEAD *** */
|
||||||
ret=http_head(filename,&lg,typebuf);
|
ret = http_head(filename, &lg, typebuf);
|
||||||
RARCH_LOG("res=%d,type='%s',lg=%d\n",ret,typebuf,lg);
|
RARCH_LOG("res=%d,type='%s',lg=%d\n",ret, typebuf, lg);
|
||||||
break;
|
break;
|
||||||
case HTTP_INTF_DELETE:
|
case HTTP_INTF_DELETE:
|
||||||
/* *** DELETE *** */
|
/* *** DELETE *** */
|
||||||
ret=http_delete(filename);
|
ret = http_delete(filename);
|
||||||
RARCH_LOG("res=%d\n",ret);
|
RARCH_LOG("res=%d\n", ret);
|
||||||
break;
|
break;
|
||||||
/* impossible... */
|
/* impossible... */
|
||||||
default:
|
default:
|
||||||
|
101
http_lib.c
101
http_lib.c
@ -77,10 +77,11 @@ static const char *http_user_agent="adlib/3 ($Date: 1998/09/23 06:19:15 $)";
|
|||||||
static int http_read_line (int fd, char *buffer, int max)
|
static int http_read_line (int fd, char *buffer, int max)
|
||||||
{
|
{
|
||||||
/* not efficient on long lines (multiple unbuffered 1 char reads) */
|
/* not efficient on long lines (multiple unbuffered 1 char reads) */
|
||||||
int n=0;
|
int n = 0;
|
||||||
|
|
||||||
while (n<max)
|
while (n<max)
|
||||||
{
|
{
|
||||||
if (read(fd,buffer,1) != 1)
|
if (read(fd, buffer, 1) != 1)
|
||||||
{
|
{
|
||||||
n= -n;
|
n= -n;
|
||||||
break;
|
break;
|
||||||
@ -112,9 +113,10 @@ static int http_read_line (int fd, char *buffer, int max)
|
|||||||
static int http_read_buffer (int fd, char *buffer, int length)
|
static int http_read_buffer (int fd, char *buffer, int length)
|
||||||
{
|
{
|
||||||
int n,r;
|
int n,r;
|
||||||
|
|
||||||
for (n = 0; n < length; n += r)
|
for (n = 0; n < length; n += r)
|
||||||
{
|
{
|
||||||
r = read(fd,buffer,length-n);
|
r = read(fd, buffer, length-n);
|
||||||
if (r <= 0)
|
if (r <= 0)
|
||||||
return -n;
|
return -n;
|
||||||
buffer += r;
|
buffer += r;
|
||||||
@ -160,7 +162,8 @@ static http_retcode http_query(const char *command, const char *url,
|
|||||||
length - size of data
|
length - size of data
|
||||||
pfd - pointer to variable where to set file descriptor value
|
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)
|
const char *data, int length, int *pfd)
|
||||||
{
|
{
|
||||||
int s;
|
int s;
|
||||||
@ -169,10 +172,11 @@ static http_retcode http_query(const char *command, const char *url, const char
|
|||||||
char header[MAXBUF];
|
char header[MAXBUF];
|
||||||
int hlg;
|
int hlg;
|
||||||
http_retcode ret;
|
http_retcode ret;
|
||||||
int proxy=(http_proxy_server != NULL && http_proxy_port != 0);
|
int proxy = (http_proxy_server != NULL && http_proxy_port != 0);
|
||||||
int port = proxy ? http_proxy_port : http_port ;
|
int port = proxy ? http_proxy_port : http_port ;
|
||||||
|
|
||||||
if (pfd) *pfd=-1;
|
if (pfd)
|
||||||
|
*pfd=-1;
|
||||||
|
|
||||||
/* get host info by name :*/
|
/* get host info by name :*/
|
||||||
if ((hp = gethostbyname( proxy ? http_proxy_server
|
if ((hp = gethostbyname( proxy ? http_proxy_server
|
||||||
@ -182,8 +186,9 @@ static http_retcode http_query(const char *command, const char *url, const char
|
|||||||
{
|
{
|
||||||
memset((char *) &server,0, sizeof(server));
|
memset((char *) &server,0, sizeof(server));
|
||||||
memmove((char *) &server.sin_addr, hp->h_addr, hp->h_length);
|
memmove((char *) &server.sin_addr, hp->h_addr, hp->h_length);
|
||||||
|
|
||||||
server.sin_family = hp->h_addrtype;
|
server.sin_family = hp->h_addrtype;
|
||||||
server.sin_port = (unsigned short) htons( port );
|
server.sin_port = (unsigned short) htons( port );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
return ERRHOST;
|
return ERRHOST;
|
||||||
@ -229,11 +234,11 @@ static http_retcode http_query(const char *command, const char *url, const char
|
|||||||
hlg=strlen(header);
|
hlg=strlen(header);
|
||||||
|
|
||||||
/* send header */
|
/* send header */
|
||||||
if (write(s,header,hlg) != hlg)
|
if (write(s, header, hlg) != hlg)
|
||||||
ret= ERRWRHD;
|
ret= ERRWRHD;
|
||||||
|
|
||||||
/* send data */
|
/* send data */
|
||||||
else if (length && data && (write(s,data,length) != length) )
|
else if (length && data && (write(s, data, length) != length) )
|
||||||
ret= ERRWRDT;
|
ret= ERRWRDT;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -245,7 +250,7 @@ static http_retcode http_query(const char *command, const char *url, const char
|
|||||||
#endif
|
#endif
|
||||||
if (linelen<=0)
|
if (linelen<=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;
|
||||||
@ -276,20 +281,17 @@ static http_retcode http_query(const char *command, const char *url, const char
|
|||||||
was already existing.
|
was already existing.
|
||||||
type - type of the data, if NULL default type is used.
|
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];
|
char header[MAXBUF];
|
||||||
if (type)
|
if (type)
|
||||||
sprintf(header,"Content-length: %d\015\012Content-type: %.64s\015\012%s",
|
sprintf(header, "Content-length: %d\015\012Content-type: %.64s\015\012%s",
|
||||||
length,
|
length, type, overwrite ? "Control: overwrite=1\015\012" : "");
|
||||||
type ,
|
|
||||||
overwrite ? "Control: overwrite=1\015\012" : ""
|
|
||||||
);
|
|
||||||
else
|
else
|
||||||
sprintf(header,"Content-length: %d\015\012%s",length,
|
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);
|
||||||
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.
|
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;
|
http_retcode ret;
|
||||||
|
|
||||||
@ -336,7 +339,8 @@ http_retcode http_get(const char *filename, char **pdata, int *plength, char *ty
|
|||||||
if (typebuf)
|
if (typebuf)
|
||||||
*typebuf = '\0';
|
*typebuf = '\0';
|
||||||
|
|
||||||
ret = http_query("GET",filename,"",KEEP_OPEN, NULL, 0, &fd);
|
ret = http_query("GET", filename, "", KEEP_OPEN, NULL, 0, &fd);
|
||||||
|
|
||||||
if (ret == 200)
|
if (ret == 200)
|
||||||
{
|
{
|
||||||
while (1)
|
while (1)
|
||||||
@ -346,7 +350,7 @@ http_retcode http_get(const char *filename, char **pdata, int *plength, char *ty
|
|||||||
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;
|
||||||
@ -362,6 +366,7 @@ http_retcode http_get(const char *filename, char **pdata, int *plength, char *ty
|
|||||||
if (typebuf)
|
if (typebuf)
|
||||||
sscanf(header,"content-type: %s",typebuf);
|
sscanf(header,"content-type: %s",typebuf);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (length<=0)
|
if (length<=0)
|
||||||
{
|
{
|
||||||
close(fd);
|
close(fd);
|
||||||
@ -369,18 +374,19 @@ http_retcode http_get(const char *filename, char **pdata, int *plength, char *ty
|
|||||||
}
|
}
|
||||||
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;
|
||||||
}
|
}
|
||||||
else if (ret>=0)
|
else if (ret >= 0)
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -419,31 +425,34 @@ http_retcode http_head(const char *filename, int *plength, char *typebuf)
|
|||||||
if (typebuf)
|
if (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)
|
||||||
{
|
{
|
||||||
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);
|
||||||
#endif
|
#endif
|
||||||
if (n<=0)
|
if (n<=0)
|
||||||
{
|
{
|
||||||
close(fd);
|
close(fd);
|
||||||
return ERRRDHD;
|
return ERRRDHD;
|
||||||
}
|
}
|
||||||
/* empty line ? (=> end of header) */
|
|
||||||
|
/* Empty line ? (=> end of header) */
|
||||||
if (n > 0 && (*header) == '\0')
|
if (n > 0 && (*header) == '\0')
|
||||||
break;
|
break;
|
||||||
/* 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;
|
||||||
@ -472,7 +481,7 @@ http_retcode http_head(const char *filename, int *plength, char *typebuf)
|
|||||||
|
|
||||||
http_retcode http_delete(const char *filename)
|
http_retcode http_delete(const char *filename)
|
||||||
{
|
{
|
||||||
return http_query("DELETE",filename,"",CLOSE, NULL, 0, NULL);
|
return http_query("DELETE", filename, "", CLOSE, NULL, 0, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* parses an url : setting the http_server and http_port global variables
|
/* parses an url : setting the http_server and http_port global variables
|
||||||
@ -487,9 +496,10 @@ http_retcode http_delete(const char *filename)
|
|||||||
*/
|
*/
|
||||||
http_retcode http_parse_url(char *url, char **pfilename)
|
http_retcode http_parse_url(char *url, char **pfilename)
|
||||||
{
|
{
|
||||||
char *pc,c;
|
char *pc, c;
|
||||||
|
|
||||||
|
http_port = 80;
|
||||||
|
|
||||||
http_port=80;
|
|
||||||
if (http_server)
|
if (http_server)
|
||||||
{
|
{
|
||||||
free(http_server);
|
free(http_server);
|
||||||
@ -501,7 +511,7 @@ http_retcode http_parse_url(char *url, char **pfilename)
|
|||||||
*pfilename=NULL;
|
*pfilename=NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strncasecmp("http://",url,7))
|
if (strncasecmp("http://", url, 7))
|
||||||
{
|
{
|
||||||
#ifdef VERBOSE
|
#ifdef VERBOSE
|
||||||
fprintf(stderr,"invalid url (must start with 'http://')\n");
|
fprintf(stderr,"invalid url (must start with 'http://')\n");
|
||||||
@ -509,12 +519,15 @@ http_retcode http_parse_url(char *url, char **pfilename)
|
|||||||
return ERRURLH;
|
return ERRURLH;
|
||||||
}
|
}
|
||||||
url+=7;
|
url+=7;
|
||||||
|
|
||||||
for (pc = url, c = *pc; (c && c != ':' && c != '/');)
|
for (pc = url, c = *pc; (c && c != ':' && c != '/');)
|
||||||
c=*pc++;
|
c=*pc++;
|
||||||
|
|
||||||
*(pc-1) = 0;
|
*(pc-1) = 0;
|
||||||
|
|
||||||
if (c == ':')
|
if (c == ':')
|
||||||
{
|
{
|
||||||
if (sscanf(pc,"%d",&http_port) != 1)
|
if (sscanf(pc, "%d", &http_port) != 1)
|
||||||
{
|
{
|
||||||
#ifdef VERBOSE
|
#ifdef VERBOSE
|
||||||
fprintf(stderr,"invalid port in url\n");
|
fprintf(stderr,"invalid port in url\n");
|
||||||
@ -526,12 +539,12 @@ http_retcode http_parse_url(char *url, char **pfilename)
|
|||||||
pc++;
|
pc++;
|
||||||
}
|
}
|
||||||
|
|
||||||
http_server=strdup(url);
|
http_server = strdup(url);
|
||||||
*pfilename= strdup ( c ? pc : "") ;
|
*pfilename = strdup ( c ? pc : "") ;
|
||||||
|
|
||||||
#ifdef VERBOSE
|
#ifdef VERBOSE
|
||||||
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 OK0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user