2015-01-23 00:45:38 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
|
|
|
* Copyright (C) 2011-2015 - Daniel De Matteis
|
|
|
|
* Copyright (C) 2014-2015 - Alfred Agrell
|
|
|
|
*
|
|
|
|
* RetroArch is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
2015-01-23 00:20:56 +00:00
|
|
|
*
|
2015-01-23 00:45:38 +00:00
|
|
|
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with RetroArch.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2015-01-23 00:20:56 +00:00
|
|
|
|
2015-01-23 03:57:45 +00:00
|
|
|
#include "net_http.h"
|
|
|
|
#include <stdio.h>
|
2015-01-21 22:23:36 +00:00
|
|
|
#include <stdlib.h>
|
2015-01-23 03:57:45 +00:00
|
|
|
#include <ctype.h>
|
2015-01-27 23:16:08 +00:00
|
|
|
#include "net_compat.h"
|
2015-01-24 00:56:55 +00:00
|
|
|
#include <compat/strl.h>
|
2015-01-21 22:23:36 +00:00
|
|
|
|
2015-01-23 00:20:56 +00:00
|
|
|
enum
|
|
|
|
{
|
2015-01-26 19:50:49 +00:00
|
|
|
P_HEADER_TOP = 0,
|
|
|
|
P_HEADER,
|
|
|
|
P_BODY,
|
|
|
|
P_BODY_CHUNKLEN,
|
|
|
|
P_DONE,
|
|
|
|
P_ERROR
|
2015-01-23 00:20:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
2015-01-26 19:47:59 +00:00
|
|
|
T_FULL = 0,
|
|
|
|
T_LEN,
|
|
|
|
T_CHUNK
|
2015-01-23 00:20:56 +00:00
|
|
|
};
|
|
|
|
|
2015-02-16 01:50:32 +00:00
|
|
|
struct http_t
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
char part;
|
|
|
|
char bodytype;
|
|
|
|
bool error;
|
|
|
|
|
|
|
|
size_t pos;
|
|
|
|
size_t len;
|
|
|
|
size_t buflen;
|
|
|
|
char * data;
|
|
|
|
};
|
|
|
|
|
2015-02-26 04:35:58 +00:00
|
|
|
struct http_connection_t
|
2015-01-21 22:23:36 +00:00
|
|
|
{
|
2015-02-26 04:35:58 +00:00
|
|
|
char *domain;
|
|
|
|
char *location;
|
|
|
|
char *urlcopy;
|
2015-01-24 00:54:59 +00:00
|
|
|
char* scan;
|
2015-02-26 04:35:58 +00:00
|
|
|
int port;
|
|
|
|
};
|
2015-01-23 00:20:56 +00:00
|
|
|
|
2015-01-21 22:23:36 +00:00
|
|
|
|
2015-01-23 03:57:45 +00:00
|
|
|
static int net_http_new_socket(const char * domain, int port)
|
2015-01-21 22:23:36 +00:00
|
|
|
{
|
2015-01-23 06:31:34 +00:00
|
|
|
int fd, i = 1;
|
2015-01-23 07:07:15 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
u_long mode = 1;
|
|
|
|
#else
|
2015-01-26 19:47:59 +00:00
|
|
|
struct timeval timeout;
|
2015-01-23 00:20:56 +00:00
|
|
|
#endif
|
2015-01-26 19:47:59 +00:00
|
|
|
struct addrinfo hints, *addr = NULL;
|
|
|
|
char portstr[16];
|
|
|
|
|
|
|
|
snprintf(portstr, sizeof(portstr), "%i", port);
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_flags = 0;
|
|
|
|
|
|
|
|
if (getaddrinfo_rarch(domain, portstr, &hints, &addr) < 0)
|
2015-01-23 00:20:56 +00:00
|
|
|
return -1;
|
2015-01-26 19:47:59 +00:00
|
|
|
|
2015-01-23 23:07:39 +00:00
|
|
|
(void)i;
|
2015-01-26 19:47:59 +00:00
|
|
|
|
|
|
|
fd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
|
2015-01-23 00:20:56 +00:00
|
|
|
|
2015-01-21 22:23:36 +00:00
|
|
|
#ifndef _WIN32
|
2015-01-26 19:47:59 +00:00
|
|
|
timeout.tv_sec=4;
|
|
|
|
timeout.tv_usec=0;
|
|
|
|
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char*)&timeout, sizeof timeout);
|
2015-01-21 22:23:36 +00:00
|
|
|
#endif
|
2015-01-23 00:20:56 +00:00
|
|
|
|
2015-01-26 19:47:59 +00:00
|
|
|
if (connect(fd, addr->ai_addr, addr->ai_addrlen) != 0)
|
|
|
|
{
|
|
|
|
freeaddrinfo_rarch(addr);
|
|
|
|
socket_close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
2015-01-23 00:20:56 +00:00
|
|
|
|
2015-01-26 19:47:59 +00:00
|
|
|
freeaddrinfo_rarch(addr);
|
2015-01-23 06:31:34 +00:00
|
|
|
|
2015-01-23 10:11:22 +00:00
|
|
|
if (!socket_nonblock(fd))
|
|
|
|
{
|
2015-01-26 19:47:59 +00:00
|
|
|
socket_close(fd);
|
2015-01-23 10:11:22 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2015-01-23 00:20:56 +00:00
|
|
|
|
2015-01-26 19:47:59 +00:00
|
|
|
return fd;
|
2015-01-21 22:23:36 +00:00
|
|
|
}
|
|
|
|
|
2015-01-23 03:57:45 +00:00
|
|
|
static void net_http_send(int fd, bool * error,
|
2015-01-23 00:20:56 +00:00
|
|
|
const char * data, size_t len)
|
2015-01-21 22:23:36 +00:00
|
|
|
{
|
2015-01-26 19:47:59 +00:00
|
|
|
if (*error)
|
2015-01-23 00:20:56 +00:00
|
|
|
return;
|
|
|
|
|
2015-01-26 19:47:59 +00:00
|
|
|
while (len)
|
|
|
|
{
|
|
|
|
ssize_t thislen = send(fd, data, len, MSG_NOSIGNAL);
|
2015-01-23 00:20:56 +00:00
|
|
|
|
2015-01-26 19:47:59 +00:00
|
|
|
if (thislen <= 0)
|
2015-01-23 00:20:56 +00:00
|
|
|
{
|
|
|
|
if (!isagain(thislen))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
*error=true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-26 19:47:59 +00:00
|
|
|
data += thislen;
|
|
|
|
len -= thislen;
|
|
|
|
}
|
2015-01-21 22:23:36 +00:00
|
|
|
}
|
|
|
|
|
2015-01-23 03:57:45 +00:00
|
|
|
static void net_http_send_str(int fd, bool *error, const char *text)
|
2015-01-21 22:23:36 +00:00
|
|
|
{
|
2015-01-26 19:47:59 +00:00
|
|
|
net_http_send(fd, error, text, strlen(text));
|
2015-01-21 22:23:36 +00:00
|
|
|
}
|
|
|
|
|
2015-01-23 03:57:45 +00:00
|
|
|
static ssize_t net_http_recv(int fd, bool *error,
|
2015-01-23 00:20:56 +00:00
|
|
|
uint8_t *data, size_t maxlen)
|
2015-01-21 22:23:36 +00:00
|
|
|
{
|
2015-01-23 00:20:56 +00:00
|
|
|
ssize_t bytes;
|
|
|
|
|
|
|
|
if (*error)
|
|
|
|
return -1;
|
|
|
|
|
2015-01-23 04:11:15 +00:00
|
|
|
bytes = recv(fd, (char*)data, maxlen, 0);
|
2015-01-23 00:20:56 +00:00
|
|
|
|
|
|
|
if (bytes > 0)
|
|
|
|
return bytes;
|
|
|
|
else if (bytes == 0)
|
|
|
|
return -1;
|
|
|
|
else if (isagain(bytes))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*error=true;
|
|
|
|
return -1;
|
2015-01-21 22:23:36 +00:00
|
|
|
}
|
|
|
|
|
2015-02-26 04:35:58 +00:00
|
|
|
struct http_connection_t *net_http_connection_new(const char *url)
|
|
|
|
{
|
|
|
|
char **domain = NULL;
|
|
|
|
struct http_connection_t *conn = (struct http_connection_t*)calloc(1,
|
|
|
|
sizeof(struct http_connection_t));
|
|
|
|
|
|
|
|
if (!conn)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
conn->urlcopy = (char*)malloc(strlen(url) + 1);
|
|
|
|
|
|
|
|
if (!conn->urlcopy)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
strcpy(conn->urlcopy, url);
|
|
|
|
|
|
|
|
if (strncmp(url, "http://", strlen("http://")) != 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
conn->scan = conn->urlcopy + strlen("http://");
|
|
|
|
|
|
|
|
domain = &conn->domain;
|
|
|
|
|
|
|
|
*domain = conn->scan;
|
|
|
|
|
|
|
|
return conn;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (conn->urlcopy)
|
|
|
|
free(conn->urlcopy);
|
|
|
|
conn->urlcopy = NULL;
|
|
|
|
if (conn)
|
|
|
|
free(conn);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool net_http_connection_iterate(struct http_connection_t *conn)
|
|
|
|
{
|
|
|
|
if (*conn->scan != '/' && *conn->scan != ':' && *conn->scan != '\0')
|
|
|
|
{
|
|
|
|
conn->scan++;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool net_http_connection_done(struct http_connection_t *conn)
|
|
|
|
{
|
|
|
|
char **location = NULL;
|
|
|
|
|
|
|
|
if (!conn)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
location = &conn->location;
|
|
|
|
|
|
|
|
if (*conn->scan == '\0')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*conn->scan = '\0';
|
|
|
|
conn->port = 80;
|
|
|
|
|
|
|
|
if (*conn->scan == ':')
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!isdigit(conn->scan[1]))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
conn->port = strtoul(conn->scan + 1, &conn->scan, 10);
|
|
|
|
|
|
|
|
if (*conn->scan != '/')
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*location = conn->scan + 1;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void net_http_connection_free(struct http_connection_t *conn)
|
|
|
|
{
|
|
|
|
if (conn->urlcopy)
|
|
|
|
free(conn->urlcopy);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct http_t *net_http_new(struct http_connection_t *conn)
|
2015-01-21 22:23:36 +00:00
|
|
|
{
|
2015-01-26 19:47:59 +00:00
|
|
|
bool error;
|
2015-02-26 04:35:58 +00:00
|
|
|
int fd = -1;
|
2015-02-16 01:50:32 +00:00
|
|
|
struct http_t *state = NULL;
|
2015-01-23 00:20:56 +00:00
|
|
|
|
2015-02-26 04:35:58 +00:00
|
|
|
if (!conn)
|
|
|
|
goto error;
|
2015-01-23 00:20:56 +00:00
|
|
|
|
2015-02-26 04:35:58 +00:00
|
|
|
fd = net_http_new_socket(conn->domain, conn->port);
|
2015-01-26 19:47:59 +00:00
|
|
|
if (fd == -1)
|
2015-02-26 04:35:58 +00:00
|
|
|
goto error;
|
2015-01-26 19:47:59 +00:00
|
|
|
|
|
|
|
error=false;
|
|
|
|
|
|
|
|
/* This is a bit lazy, but it works. */
|
|
|
|
net_http_send_str(fd, &error, "GET /");
|
2015-02-26 04:35:58 +00:00
|
|
|
net_http_send_str(fd, &error, conn->location);
|
2015-01-26 19:47:59 +00:00
|
|
|
net_http_send_str(fd, &error, " HTTP/1.1\r\n");
|
|
|
|
|
|
|
|
net_http_send_str(fd, &error, "Host: ");
|
2015-02-26 04:35:58 +00:00
|
|
|
net_http_send_str(fd, &error, conn->domain);
|
2015-01-26 19:47:59 +00:00
|
|
|
|
2015-02-26 04:35:58 +00:00
|
|
|
if (conn->port != 80)
|
2015-01-26 19:47:59 +00:00
|
|
|
{
|
|
|
|
char portstr[16];
|
|
|
|
|
2015-02-26 04:35:58 +00:00
|
|
|
snprintf(portstr, sizeof(portstr), ":%i", conn->port);
|
2015-01-26 19:47:59 +00:00
|
|
|
net_http_send_str(fd, &error, portstr);
|
|
|
|
}
|
|
|
|
|
|
|
|
net_http_send_str(fd, &error, "\r\n");
|
|
|
|
net_http_send_str(fd, &error, "Connection: close\r\n");
|
|
|
|
net_http_send_str(fd, &error, "\r\n");
|
|
|
|
|
|
|
|
if (error)
|
2015-02-26 04:35:58 +00:00
|
|
|
goto error;
|
2015-01-26 19:47:59 +00:00
|
|
|
|
2015-02-16 01:50:32 +00:00
|
|
|
state = (struct http_t*)malloc(sizeof(struct http_t));
|
2015-01-26 19:47:59 +00:00
|
|
|
state->fd = fd;
|
|
|
|
state->status = -1;
|
|
|
|
state->data = NULL;
|
2015-01-26 19:50:49 +00:00
|
|
|
state->part = P_HEADER_TOP;
|
2015-01-26 19:47:59 +00:00
|
|
|
state->bodytype= T_FULL;
|
|
|
|
state->error = false;
|
|
|
|
state->pos = 0;
|
|
|
|
state->len = 0;
|
|
|
|
state->buflen = 512;
|
|
|
|
state->data = (char*)malloc(state->buflen);
|
|
|
|
|
2015-02-16 05:24:06 +00:00
|
|
|
if (!state->data)
|
2015-02-26 04:35:58 +00:00
|
|
|
goto error;
|
2015-02-16 05:24:06 +00:00
|
|
|
|
2015-01-26 19:47:59 +00:00
|
|
|
return state;
|
|
|
|
|
2015-02-26 04:35:58 +00:00
|
|
|
error:
|
2015-01-26 19:47:59 +00:00
|
|
|
if (fd != -1)
|
2015-01-23 10:36:45 +00:00
|
|
|
socket_close(fd);
|
2015-01-26 19:47:59 +00:00
|
|
|
return NULL;
|
2015-01-21 22:23:36 +00:00
|
|
|
}
|
|
|
|
|
2015-02-16 01:50:32 +00:00
|
|
|
int net_http_fd(struct http_t *state)
|
2015-01-21 22:23:36 +00:00
|
|
|
{
|
2015-01-26 19:47:59 +00:00
|
|
|
return state->fd;
|
2015-01-21 22:23:36 +00:00
|
|
|
}
|
|
|
|
|
2015-02-16 01:50:32 +00:00
|
|
|
bool net_http_update(struct http_t *state, size_t* progress, size_t* total)
|
2015-01-21 22:23:36 +00:00
|
|
|
{
|
2015-01-26 19:47:59 +00:00
|
|
|
ssize_t newlen = 0;
|
|
|
|
|
|
|
|
if (state->error)
|
2015-01-23 00:20:56 +00:00
|
|
|
goto fail;
|
2015-01-26 19:47:59 +00:00
|
|
|
|
2015-01-26 19:50:49 +00:00
|
|
|
if (state->part < P_BODY)
|
2015-01-26 19:47:59 +00:00
|
|
|
{
|
|
|
|
newlen = net_http_recv(state->fd, &state->error,
|
2015-01-23 00:20:56 +00:00
|
|
|
(uint8_t*)state->data + state->pos, state->buflen - state->pos);
|
|
|
|
|
2015-01-26 19:47:59 +00:00
|
|
|
if (newlen < 0)
|
2015-01-23 00:20:56 +00:00
|
|
|
goto fail;
|
|
|
|
|
2015-01-26 19:47:59 +00:00
|
|
|
if (state->pos + newlen >= state->buflen - 64)
|
|
|
|
{
|
|
|
|
state->buflen *= 2;
|
|
|
|
state->data = (char*)realloc(state->data, state->buflen);
|
|
|
|
}
|
|
|
|
state->pos += newlen;
|
2015-01-23 00:20:56 +00:00
|
|
|
|
2015-01-26 19:50:49 +00:00
|
|
|
while (state->part < P_BODY)
|
2015-01-26 19:47:59 +00:00
|
|
|
{
|
|
|
|
char *dataend = state->data + state->pos;
|
|
|
|
char *lineend = (char*)memchr(state->data, '\n', state->pos);
|
2015-01-23 00:20:56 +00:00
|
|
|
|
2015-01-26 19:47:59 +00:00
|
|
|
if (!lineend)
|
2015-01-23 00:20:56 +00:00
|
|
|
break;
|
2015-01-26 19:47:59 +00:00
|
|
|
*lineend='\0';
|
|
|
|
if (lineend != state->data && lineend[-1]=='\r')
|
2015-01-23 00:20:56 +00:00
|
|
|
lineend[-1]='\0';
|
2015-01-26 19:47:59 +00:00
|
|
|
|
2015-01-26 19:50:49 +00:00
|
|
|
if (state->part == P_HEADER_TOP)
|
2015-01-26 19:47:59 +00:00
|
|
|
{
|
|
|
|
if (strncmp(state->data, "HTTP/1.", strlen("HTTP/1."))!=0)
|
2015-01-23 00:20:56 +00:00
|
|
|
goto fail;
|
2015-01-26 19:50:49 +00:00
|
|
|
state->status = strtoul(state->data + strlen("HTTP/1.1 "), NULL, 10);
|
|
|
|
state->part = P_HEADER;
|
2015-01-26 19:47:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!strncmp(state->data, "Content-Length: ",
|
2015-01-23 00:20:56 +00:00
|
|
|
strlen("Content-Length: ")))
|
2015-01-26 19:47:59 +00:00
|
|
|
{
|
|
|
|
state->bodytype = T_LEN;
|
|
|
|
state->len = strtol(state->data +
|
2015-01-23 00:20:56 +00:00
|
|
|
strlen("Content-Length: "), NULL, 10);
|
2015-01-26 19:47:59 +00:00
|
|
|
}
|
|
|
|
if (!strcmp(state->data, "Transfer-Encoding: chunked"))
|
|
|
|
state->bodytype = T_CHUNK;
|
|
|
|
|
|
|
|
/* TODO: save headers somewhere */
|
|
|
|
if (state->data[0]=='\0')
|
|
|
|
{
|
2015-01-26 19:50:49 +00:00
|
|
|
state->part = P_BODY;
|
2015-01-26 19:47:59 +00:00
|
|
|
if (state->bodytype == T_CHUNK)
|
2015-01-26 19:50:49 +00:00
|
|
|
state->part = P_BODY_CHUNKLEN;
|
2015-01-26 19:47:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
memmove(state->data, lineend + 1, dataend-(lineend+1));
|
|
|
|
state->pos = (dataend-(lineend + 1));
|
|
|
|
}
|
2015-01-26 19:50:49 +00:00
|
|
|
if (state->part >= P_BODY)
|
2015-01-26 19:47:59 +00:00
|
|
|
{
|
|
|
|
newlen = state->pos;
|
|
|
|
state->pos = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-26 19:50:49 +00:00
|
|
|
if (state->part >= P_BODY && state->part < P_DONE)
|
2015-01-26 19:47:59 +00:00
|
|
|
{
|
|
|
|
if (!newlen)
|
|
|
|
{
|
|
|
|
newlen = net_http_recv(state->fd, &state->error,
|
2015-01-23 00:20:56 +00:00
|
|
|
(uint8_t*)state->data + state->pos,
|
|
|
|
state->buflen - state->pos);
|
|
|
|
|
2015-01-26 19:47:59 +00:00
|
|
|
if (newlen < 0)
|
|
|
|
{
|
|
|
|
if (state->bodytype == T_FULL)
|
|
|
|
{
|
2015-01-26 19:50:49 +00:00
|
|
|
state->part = P_DONE;
|
2015-01-23 21:48:31 +00:00
|
|
|
state->data = (char*)realloc(state->data, state->len);
|
2015-01-26 19:47:59 +00:00
|
|
|
}
|
|
|
|
else
|
2015-01-23 00:20:56 +00:00
|
|
|
goto fail;
|
2015-01-26 19:47:59 +00:00
|
|
|
newlen=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state->pos + newlen >= state->buflen - 64)
|
|
|
|
{
|
|
|
|
state->buflen *= 2;
|
|
|
|
state->data = (char*)realloc(state->data, state->buflen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-22 23:10:56 +00:00
|
|
|
parse_again:
|
2015-01-26 19:47:59 +00:00
|
|
|
if (state->bodytype == T_CHUNK)
|
|
|
|
{
|
2015-01-26 19:50:49 +00:00
|
|
|
if (state->part == P_BODY_CHUNKLEN)
|
2015-01-26 19:47:59 +00:00
|
|
|
{
|
|
|
|
state->pos += newlen;
|
|
|
|
if (state->pos - state->len >= 2)
|
|
|
|
{
|
|
|
|
/*
|
2015-01-23 00:20:56 +00:00
|
|
|
* len=start of chunk including \r\n
|
|
|
|
* pos=end of data
|
|
|
|
*/
|
|
|
|
|
2015-01-26 19:47:59 +00:00
|
|
|
char *fullend = state->data + state->pos;
|
|
|
|
char *end = (char*)memchr(state->data + state->len + 2,
|
2015-01-23 00:20:56 +00:00
|
|
|
'\n', state->pos - state->len - 2);
|
|
|
|
|
2015-01-26 19:47:59 +00:00
|
|
|
if (end)
|
|
|
|
{
|
|
|
|
size_t chunklen = strtoul(state->data+state->len, NULL, 16);
|
|
|
|
state->pos = state->len;
|
|
|
|
end++;
|
2015-01-23 00:20:56 +00:00
|
|
|
|
2015-01-26 19:47:59 +00:00
|
|
|
memmove(state->data+state->len, end, fullend-end);
|
2015-01-23 00:20:56 +00:00
|
|
|
|
2015-01-26 19:47:59 +00:00
|
|
|
state->len = chunklen;
|
|
|
|
newlen = (fullend - end);
|
2015-01-23 00:20:56 +00:00
|
|
|
|
|
|
|
/*
|
2015-01-26 19:47:59 +00:00
|
|
|
len=num bytes
|
|
|
|
newlen=unparsed bytes after \n
|
|
|
|
pos=start of chunk including \r\n
|
|
|
|
*/
|
|
|
|
|
2015-01-26 19:50:49 +00:00
|
|
|
state->part = P_BODY;
|
2015-01-26 19:47:59 +00:00
|
|
|
if (state->len == 0)
|
|
|
|
{
|
2015-01-26 19:50:49 +00:00
|
|
|
state->part = P_DONE;
|
|
|
|
state->len = state->pos;
|
2015-01-26 19:47:59 +00:00
|
|
|
state->data = (char*)realloc(state->data, state->len);
|
|
|
|
}
|
|
|
|
goto parse_again;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-26 19:50:49 +00:00
|
|
|
else if (state->part == P_BODY)
|
2015-01-26 19:47:59 +00:00
|
|
|
{
|
|
|
|
if ((size_t)newlen >= state->len)
|
|
|
|
{
|
|
|
|
state->pos += state->len;
|
|
|
|
newlen -= state->len;
|
|
|
|
state->len = state->pos;
|
2015-01-26 19:50:49 +00:00
|
|
|
state->part = P_BODY_CHUNKLEN;
|
2015-01-26 19:47:59 +00:00
|
|
|
goto parse_again;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
state->pos += newlen;
|
|
|
|
state->len -= newlen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
state->pos += newlen;
|
|
|
|
|
|
|
|
if (state->pos == state->len)
|
|
|
|
{
|
2015-01-26 19:50:49 +00:00
|
|
|
state->part = P_DONE;
|
2015-01-23 21:48:31 +00:00
|
|
|
state->data = (char*)realloc(state->data, state->len);
|
2015-01-26 19:47:59 +00:00
|
|
|
}
|
|
|
|
if (state->pos > state->len)
|
2015-01-23 00:20:56 +00:00
|
|
|
goto fail;
|
2015-01-26 19:47:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (progress)
|
2015-01-23 00:20:56 +00:00
|
|
|
*progress = state->pos;
|
|
|
|
|
2015-01-26 19:47:59 +00:00
|
|
|
if (total)
|
|
|
|
{
|
|
|
|
if (state->bodytype == T_LEN)
|
2015-01-23 00:20:56 +00:00
|
|
|
*total=state->len;
|
2015-01-26 19:47:59 +00:00
|
|
|
else
|
2015-01-23 00:20:56 +00:00
|
|
|
*total=0;
|
2015-01-26 19:47:59 +00:00
|
|
|
}
|
|
|
|
|
2015-01-26 19:50:49 +00:00
|
|
|
return (state->part == P_DONE);
|
2015-01-26 19:47:59 +00:00
|
|
|
|
2015-01-21 22:23:36 +00:00
|
|
|
fail:
|
2015-01-26 19:50:49 +00:00
|
|
|
state->error = true;
|
|
|
|
state->part = P_ERROR;
|
2015-01-26 19:47:59 +00:00
|
|
|
state->status = -1;
|
2015-01-23 00:20:56 +00:00
|
|
|
|
2015-01-26 19:47:59 +00:00
|
|
|
return true;
|
2015-01-21 22:23:36 +00:00
|
|
|
}
|
|
|
|
|
2015-02-16 01:50:32 +00:00
|
|
|
int net_http_status(struct http_t *state)
|
2015-01-21 22:23:36 +00:00
|
|
|
{
|
2015-01-26 19:47:59 +00:00
|
|
|
return state->status;
|
2015-01-21 22:23:36 +00:00
|
|
|
}
|
|
|
|
|
2015-02-16 01:50:32 +00:00
|
|
|
uint8_t* net_http_data(struct http_t *state, size_t* len, bool accept_error)
|
2015-01-21 22:23:36 +00:00
|
|
|
{
|
2015-01-26 19:47:59 +00:00
|
|
|
if (!accept_error &&
|
2015-01-23 00:20:56 +00:00
|
|
|
(state->error || state->status<200 || state->status>299))
|
2015-01-26 19:47:59 +00:00
|
|
|
{
|
|
|
|
if (len)
|
2015-01-23 00:20:56 +00:00
|
|
|
*len=0;
|
2015-01-26 19:47:59 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-01-23 00:20:56 +00:00
|
|
|
|
2015-01-26 19:47:59 +00:00
|
|
|
if (len)
|
2015-01-23 00:20:56 +00:00
|
|
|
*len=state->len;
|
|
|
|
|
2015-01-26 19:47:59 +00:00
|
|
|
return (uint8_t*)state->data;
|
2015-01-21 22:23:36 +00:00
|
|
|
}
|
|
|
|
|
2015-02-16 01:50:32 +00:00
|
|
|
void net_http_delete(struct http_t *state)
|
2015-01-21 22:23:36 +00:00
|
|
|
{
|
2015-01-26 19:47:59 +00:00
|
|
|
if (state->fd != -1)
|
2015-01-23 10:36:45 +00:00
|
|
|
socket_close(state->fd);
|
2015-01-26 19:47:59 +00:00
|
|
|
if (state->data)
|
2015-01-23 00:20:56 +00:00
|
|
|
free(state->data);
|
2015-01-26 19:47:59 +00:00
|
|
|
free(state);
|
2015-01-21 22:23:36 +00:00
|
|
|
}
|