#include <ctype.h>
#include <unistd.h>
#include <stdint.h>
+#include <fcntl.h>
#include <libubox/ustream.h>
#include <libubox/ustream-ssl.h>
[__PREFIX_MAX] = NULL
};
+static int uclient_http_connect(struct uclient *cl);
+
static int uclient_do_connect(struct uclient_http *uh, const char *port)
{
socklen_t sl;
memset(&uh->uc.remote_addr, 0, sizeof(uh->uc.remote_addr));
- fd = usock_inet(USOCK_TCP | USOCK_NONBLOCK, uh->uc.url->host, port, &uh->uc.remote_addr);
+ fd = usock_inet(USOCK_TCP, uh->uc.url->host, port, &uh->uc.remote_addr);
if (fd < 0)
return -1;
+ fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
ustream_fd_init(&uh->ufd, fd);
sl = sizeof(uh->uc.local_addr);
static void uclient_http_init_request(struct uclient_http *uh)
{
+ uh->seq++;
uclient_http_reset_state(uh);
blob_buf_init(&uh->meta, 0);
}
if (uh->state >= HTTP_STATE_HEADERS_SENT)
return;
+ if (uh->uc.proxy_url)
+ url = uh->uc.proxy_url;
+
ustream_printf(uh->us,
"%s %s HTTP/1.1\r\n"
"Host: %s\r\n",
static void uclient_http_headers_complete(struct uclient_http *uh)
{
enum auth_type auth_type = uh->auth_type;
+ int seq = uh->uc.seq;
uh->state = HTTP_STATE_RECV_DATA;
uh->uc.meta = uh->meta.head;
if (auth_type == AUTH_TYPE_UNKNOWN && uh->uc.status_code == 401 &&
(uh->req_type == REQ_HEAD || uh->req_type == REQ_GET)) {
- uclient_http_init_request(uh);
+ uclient_http_connect(&uh->uc);
uclient_http_send_headers(uh);
uh->state = HTTP_STATE_REQUEST_DONE;
return;
if (uh->uc.cb->header_done)
uh->uc.cb->header_done(&uh->uc);
- if (uh->eof)
+ if (uh->eof || seq != uh->uc.seq)
return;
if (uh->req_type == REQ_HEAD || uh->uc.status_code == 204) {
{
struct uclient_http *uh = container_of(us, struct uclient_http, ufd.stream);
+ if (uh->ufd.stream.write_error) {
+ uclient_http_error(uh, UCLIENT_ERROR_CONNECT);
+ return;
+ }
uclient_notify_eof(uh);
}
struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
int ret;
- if (!cl->eof || uh->disconnect)
+ if (!cl->eof || uh->disconnect || uh->connection_close)
uclient_http_disconnect(uh);
uclient_http_init_request(uh);
return len;
}
-bool uclient_http_redirect(struct uclient *cl)
+int uclient_http_redirect(struct uclient *cl)
{
struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
struct blobmsg_policy location = {
free(cl->url);
cl->url = url;
- uclient_http_connect(cl);
+ if (uclient_http_connect(cl))
+ return -1;
+
uclient_http_request_done(cl);
return true;
.connect = uclient_http_connect,
.disconnect = uclient_http_request_disconnect,
.update_url = uclient_http_free_url_state,
+ .update_proxy_url = uclient_http_free_url_state,
.read = uclient_http_read,
.write = uclient_http_send_data,