(net_http) Updates

This commit is contained in:
twinaphex 2015-01-23 05:08:34 +01:00
parent 878eb8d2fa
commit 96ea687f7c
3 changed files with 43 additions and 53 deletions

View File

@ -47,24 +47,6 @@
#define isagain(bytes) (bytes<0 && (errno==EAGAIN || errno==EWOULDBLOCK))
#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
{
p_header_top,
@ -215,12 +197,13 @@ static ssize_t net_http_recv(int fd, bool *error,
return -1;
}
struct http* net_http_new(const char * url)
http_t *net_http_new(const char * url)
{
bool error;
char *domain, *location;
int port, fd = -1;
struct http* state = NULL;
http_t *state = NULL;
char *urlcopy =(char*)malloc(strlen(url)+1);
strcpy(urlcopy, url);
@ -259,7 +242,7 @@ struct http* net_http_new(const char * url)
free(urlcopy);
state = (struct http*)malloc(sizeof(struct http));
state = (http_t*)malloc(sizeof(http_t));
state->fd = fd;
state->status = -1;
state->data = NULL;
@ -280,12 +263,12 @@ fail:
return NULL;
}
int net_http_fd(struct http *state)
int net_http_fd(http_t *state)
{
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;
@ -475,12 +458,12 @@ fail:
return true;
}
int net_http_status(struct http* state)
int net_http_status(http_t *state)
{
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 &&
(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;
}
void net_http_delete(struct http* state)
void net_http_delete(http_t *state)
{
if (state->fd != -1)
close(state->fd);

View File

@ -25,29 +25,45 @@
extern "C" {
#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
* 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.
* '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. */
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
* HTTP handler; it's freed by net_http_delete.
*
* 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. */
void net_http_delete(struct http* state);
void net_http_delete(http_t *state);
#ifdef __cplusplus
}

View File

@ -1,35 +1,26 @@
#include <stdio.h>
#include "http_parser.h"
#include "net_http.h"
int main(void)
{
char *w;
struct http* http1, *http2, *http3;
http_t *http1, *http2, *http3;
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);
http3 = http_new("http://www.wikipedia.org/");
while (!http_update(http3, NULL, NULL)) {}
http3 = net_http_new("http://www.wikipedia.org/");
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);
#if 0
struct http* http1=http_new("http://floating.muncher.se:22/");
struct http* http2=http_new("http://floating.muncher.se/sepulcher/");
struct http* http3=http_new("http://www.wikipedia.org/");
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);
net_http_delete(http1);
net_http_delete(http3);
return 0;
}