mirror of
https://github.com/libretro/RetroArch
synced 2025-04-07 13:23:32 +00:00
(net_http) Updates
This commit is contained in:
parent
878eb8d2fa
commit
96ea687f7c
35
net_http.c
35
net_http.c
@ -47,24 +47,6 @@
|
|||||||
#define isagain(bytes) (bytes<0 && (errno==EAGAIN || errno==EWOULDBLOCK))
|
#define isagain(bytes) (bytes<0 && (errno==EAGAIN || errno==EWOULDBLOCK))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
struct http
|
|
||||||
{
|
|
||||||
int fd;
|
|
||||||
int status;
|
|
||||||
|
|
||||||
char part;
|
|
||||||
char bodytype;
|
|
||||||
bool error;
|
|
||||||
#if 0
|
|
||||||
char padding[5];
|
|
||||||
#endif
|
|
||||||
|
|
||||||
size_t pos;
|
|
||||||
size_t len;
|
|
||||||
size_t buflen;
|
|
||||||
char * data;
|
|
||||||
};
|
|
||||||
|
|
||||||
enum
|
enum
|
||||||
{
|
{
|
||||||
p_header_top,
|
p_header_top,
|
||||||
@ -215,12 +197,13 @@ static ssize_t net_http_recv(int fd, bool *error,
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct http* net_http_new(const char * url)
|
http_t *net_http_new(const char * url)
|
||||||
{
|
{
|
||||||
bool error;
|
bool error;
|
||||||
char *domain, *location;
|
char *domain, *location;
|
||||||
|
|
||||||
int port, fd = -1;
|
int port, fd = -1;
|
||||||
struct http* state = NULL;
|
http_t *state = NULL;
|
||||||
char *urlcopy =(char*)malloc(strlen(url)+1);
|
char *urlcopy =(char*)malloc(strlen(url)+1);
|
||||||
|
|
||||||
strcpy(urlcopy, url);
|
strcpy(urlcopy, url);
|
||||||
@ -259,7 +242,7 @@ struct http* net_http_new(const char * url)
|
|||||||
|
|
||||||
free(urlcopy);
|
free(urlcopy);
|
||||||
|
|
||||||
state = (struct http*)malloc(sizeof(struct http));
|
state = (http_t*)malloc(sizeof(http_t));
|
||||||
state->fd = fd;
|
state->fd = fd;
|
||||||
state->status = -1;
|
state->status = -1;
|
||||||
state->data = NULL;
|
state->data = NULL;
|
||||||
@ -280,12 +263,12 @@ fail:
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int net_http_fd(struct http *state)
|
int net_http_fd(http_t *state)
|
||||||
{
|
{
|
||||||
return state->fd;
|
return state->fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool net_http_update(struct http* state, size_t* progress, size_t* total)
|
bool net_http_update(http_t *state, size_t* progress, size_t* total)
|
||||||
{
|
{
|
||||||
ssize_t newlen = 0;
|
ssize_t newlen = 0;
|
||||||
|
|
||||||
@ -475,12 +458,12 @@ fail:
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
int net_http_status(struct http* state)
|
int net_http_status(http_t *state)
|
||||||
{
|
{
|
||||||
return state->status;
|
return state->status;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t* net_http_data(struct http* state, size_t* len, bool accept_error)
|
uint8_t* net_http_data(http_t *state, size_t* len, bool accept_error)
|
||||||
{
|
{
|
||||||
if (!accept_error &&
|
if (!accept_error &&
|
||||||
(state->error || state->status<200 || state->status>299))
|
(state->error || state->status<200 || state->status>299))
|
||||||
@ -496,7 +479,7 @@ uint8_t* net_http_data(struct http* state, size_t* len, bool accept_error)
|
|||||||
return (uint8_t*)state->data;
|
return (uint8_t*)state->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
void net_http_delete(struct http* state)
|
void net_http_delete(http_t *state)
|
||||||
{
|
{
|
||||||
if (state->fd != -1)
|
if (state->fd != -1)
|
||||||
close(state->fd);
|
close(state->fd);
|
||||||
|
30
net_http.h
30
net_http.h
@ -25,29 +25,45 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
struct http;
|
typedef struct
|
||||||
|
{
|
||||||
|
int fd;
|
||||||
|
int status;
|
||||||
|
|
||||||
|
char part;
|
||||||
|
char bodytype;
|
||||||
|
bool error;
|
||||||
|
#if 0
|
||||||
|
char padding[5];
|
||||||
|
#endif
|
||||||
|
|
||||||
|
size_t pos;
|
||||||
|
size_t len;
|
||||||
|
size_t buflen;
|
||||||
|
char * data;
|
||||||
|
} http_t;
|
||||||
|
|
||||||
struct http* net_http_new(const char * url);
|
http_t *net_http_new(const char * url);
|
||||||
|
|
||||||
/* You can use this to call net_http_update
|
/* You can use this to call net_http_update
|
||||||
* only when something will happen; select() it for reading. */
|
* only when something will happen; select() it for reading. */
|
||||||
int net_http_fd(struct http* state);
|
int net_http_fd(http_t *state);
|
||||||
|
|
||||||
/* Returns true if it's done, or if something broke.
|
/* Returns true if it's done, or if something broke.
|
||||||
* 'total' will be 0 if it's not known. */
|
* 'total' will be 0 if it's not known. */
|
||||||
bool net_http_update(struct http* state, size_t* progress, size_t* total);
|
bool net_http_update(http_t *state, size_t* progress, size_t* total);
|
||||||
|
|
||||||
/* 200, 404, or whatever. */
|
/* 200, 404, or whatever. */
|
||||||
int net_http_status(struct http* state);
|
int net_http_status(http_t *state);
|
||||||
|
|
||||||
/* Returns the downloaded data. The returned buffer is owned by the
|
/* Returns the downloaded data. The returned buffer is owned by the
|
||||||
* HTTP handler; it's freed by net_http_delete.
|
* HTTP handler; it's freed by net_http_delete.
|
||||||
*
|
*
|
||||||
* If the status is not 20x and accept_error is false, it returns NULL. */
|
* If the status is not 20x and accept_error is false, it returns NULL. */
|
||||||
uint8_t* net_http_data(struct http* state, size_t* len, bool accept_error);
|
uint8_t* net_http_data(http_t *state, size_t* len, bool accept_error);
|
||||||
|
|
||||||
/* Cleans up all memory. */
|
/* Cleans up all memory. */
|
||||||
void net_http_delete(struct http* state);
|
void net_http_delete(http_t *state);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
@ -1,35 +1,26 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "http_parser.h"
|
#include "net_http.h"
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
char *w;
|
char *w;
|
||||||
struct http* http1, *http2, *http3;
|
http_t *http1, *http2, *http3;
|
||||||
size_t q, pos = 0; size_t tot = 0;
|
size_t q, pos = 0; size_t tot = 0;
|
||||||
|
|
||||||
http1 = http_new("http://buildbot.libretro.com/nightly/win-x86/latest/mednafen_psx_libretro.dll.zip");
|
http1 = net_http_new("http://buildbot.libretro.com/nightly/win-x86/latest/mednafen_psx_libretro.dll.zip");
|
||||||
|
|
||||||
while (!http_update(http1, &pos, &tot))
|
while (!net_http_update(http1, &pos, &tot))
|
||||||
printf("%.9lu / %.9lu \r",pos,tot);
|
printf("%.9lu / %.9lu \r",pos,tot);
|
||||||
|
|
||||||
http3 = http_new("http://www.wikipedia.org/");
|
http3 = net_http_new("http://www.wikipedia.org/");
|
||||||
while (!http_update(http3, NULL, NULL)) {}
|
while (!net_http_update(http3, NULL, NULL)) {}
|
||||||
|
|
||||||
w = (char*)http_data(http3, &q, false);
|
w = (char*)net_http_data(http3, &q, false);
|
||||||
|
|
||||||
printf("%.*s\n", (int)256, w);
|
printf("%.*s\n", (int)256, w);
|
||||||
|
|
||||||
#if 0
|
net_http_delete(http1);
|
||||||
struct http* http1=http_new("http://floating.muncher.se:22/");
|
net_http_delete(http3);
|
||||||
struct http* http2=http_new("http://floating.muncher.se/sepulcher/");
|
|
||||||
struct http* http3=http_new("http://www.wikipedia.org/");
|
return 0;
|
||||||
while (!http_update(http3, NULL, NULL)) {}
|
|
||||||
while (!http_update(http2, NULL, NULL)) {}
|
|
||||||
while (!http_update(http1, NULL, NULL)) {}
|
|
||||||
printf("%i %i %i %p %s %s\n",
|
|
||||||
http_status(http1),http_status(http2),http_status(http3),
|
|
||||||
(char*)http_data(http1, NULL, false),http_data(http2, NULL, true),http_data(http3, NULL, true));
|
|
||||||
#endif
|
|
||||||
http_delete(http1);
|
|
||||||
http_delete(http3);
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user