X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fuclient.git;a=blobdiff_plain;f=uclient-http.c;h=16ffe9660cd55a0cd50af94565e0aa5dd55aa92b;hp=83eac58f1ea9e24fece60ac7ac259378c0893fa6;hb=cc75af25cbaee8a57b799b6ecc5a7c2d624cc431;hpb=63a984dbdf8a4d1249838e0708a4bed2dfaff476;ds=sidebyside diff --git a/uclient-http.c b/uclient-http.c index 83eac58..16ffe96 100644 --- a/uclient-http.c +++ b/uclient-http.c @@ -61,17 +61,20 @@ static const char * const request_types[__REQ_MAX] = { struct uclient_http { struct uclient uc; + const struct ustream_ssl_ops *ssl_ops; struct ustream_ssl_ctx *ssl_ctx; struct ustream *us; struct ustream_fd ufd; struct ustream_ssl ussl; + struct uloop_timeout disconnect_t; + bool ssl_require_validation; - bool ssl_ctx_ext; bool ssl; bool eof; bool connection_close; + bool disconnect; enum request_type req_type; enum http_state state; @@ -125,6 +128,7 @@ static int uclient_do_connect(struct uclient_http *uh, const char *port) static void uclient_http_disconnect(struct uclient_http *uh) { + uloop_timeout_cancel(&uh->disconnect_t); if (!uh->us) return; @@ -157,6 +161,9 @@ static void uclient_notify_eof(struct uclient_http *uh) { struct ustream *us = uh->us; + if (uh->disconnect) + return; + if (!uh->eof) { if (!us->eof && !us->write_error) return; @@ -177,6 +184,7 @@ static void uclient_http_reset_state(struct uclient_http *uh) uh->read_chunked = -1; uh->content_length = -1; uh->eof = false; + uh->disconnect = false; uh->connection_close = false; uh->state = HTTP_STATE_INIT; @@ -527,6 +535,9 @@ static void uclient_http_headers_complete(struct uclient_http *uh) if (uh->uc.cb->header_done) uh->uc.cb->header_done(&uh->uc); + if (uh->eof) + return; + if (uh->req_type == REQ_HEAD || uh->uc.status_code == 204) { uh->eof = true; uclient_notify_eof(uh); @@ -623,6 +634,9 @@ static void __uclient_notify_read(struct uclient_http *uh) ustream_consume(uh->us, cur_len); len -= cur_len; + if (uh->eof) + return; + data = ustream_get_read_buf(uh->us, &len); } while (data && uh->state < HTTP_STATE_RECV_DATA); @@ -630,6 +644,9 @@ static void __uclient_notify_read(struct uclient_http *uh) return; } + if (uh->eof) + return; + if (uh->state == HTTP_STATE_RECV_DATA && uc->cb->data_read) uc->cb->data_read(uc); } @@ -654,13 +671,15 @@ static int uclient_setup_http(struct uclient_http *uh) int ret; uh->us = us; + uh->ssl = false; + us->string_data = true; us->notify_state = uclient_notify_state; us->notify_read = uclient_notify_read; ret = uclient_do_connect(uh, "80"); if (ret) - return ret; + return UCLIENT_ERROR_CONNECT; return 0; } @@ -715,12 +734,12 @@ static int uclient_setup_https(struct uclient_http *uh) uh->ssl = true; uh->us = us; + if (!uh->ssl_ctx) + return UCLIENT_ERROR_MISSING_SSL_CONTEXT; + ret = uclient_do_connect(uh, "443"); if (ret) - return ret; - - if (!uh->ssl_ctx) - uh->ssl_ctx = ustream_ssl_context_new(false); + return UCLIENT_ERROR_CONNECT; us->string_data = true; us->notify_state = uclient_ssl_notify_state; @@ -728,8 +747,8 @@ static int uclient_setup_https(struct uclient_http *uh) uh->ussl.notify_error = uclient_ssl_notify_error; uh->ussl.notify_verify_error = uclient_ssl_notify_verify_error; uh->ussl.notify_connected = uclient_ssl_notify_connected; - ustream_ssl_init(&uh->ussl, &uh->ufd.stream, uh->ssl_ctx, false); - ustream_ssl_set_peer_cn(&uh->ussl, uh->uc.url->host); + uh->ssl_ops->init(&uh->ussl, &uh->ufd.stream, uh->ssl_ctx, false); + uh->ssl_ops->set_peer_cn(&uh->ussl, uh->uc.url->host); return 0; } @@ -751,17 +770,22 @@ static int uclient_http_connect(struct uclient *cl) else ret = uclient_setup_http(uh); - if (ret) - uclient_http_error(uh, UCLIENT_ERROR_CONNECT); - return ret; } +static void uclient_http_disconnect_cb(struct uloop_timeout *timeout) +{ + struct uclient_http *uh = container_of(timeout, struct uclient_http, disconnect_t); + + uclient_http_disconnect(uh); +} + static struct uclient *uclient_http_alloc(void) { struct uclient_http *uh; uh = calloc_a(sizeof(*uh)); + uh->disconnect_t.cb = uclient_http_disconnect_cb; blob_buf_init(&uh->headers, 0); return &uh->uc; @@ -769,18 +793,16 @@ static struct uclient *uclient_http_alloc(void) static void uclient_http_free_ssl_ctx(struct uclient_http *uh) { - if (uh->ssl_ctx && !uh->ssl_ctx_ext) - ustream_ssl_context_free(uh->ssl_ctx); - - uh->ssl_ctx_ext = false; + uh->ssl_ops = NULL; + uh->ssl_ctx = NULL; } static void uclient_http_free(struct uclient *cl) { struct uclient_http *uh = container_of(cl, struct uclient_http, uc); - uclient_http_free_ssl_ctx(uh); uclient_http_free_url_state(cl); + uclient_http_free_ssl_ctx(uh); blob_buf_free(&uh->headers); blob_buf_free(&uh->meta); free(uh); @@ -976,7 +998,8 @@ bool uclient_http_redirect(struct uclient *cl) return true; } -int uclient_http_set_ssl_ctx(struct uclient *cl, struct ustream_ssl_ctx *ctx, bool require_validation) +int uclient_http_set_ssl_ctx(struct uclient *cl, const struct ustream_ssl_ops *ops, + struct ustream_ssl_ctx *ctx, bool require_validation) { struct uclient_http *uh = container_of(cl, struct uclient_http, uc); @@ -986,19 +1009,32 @@ int uclient_http_set_ssl_ctx(struct uclient *cl, struct ustream_ssl_ctx *ctx, bo uclient_http_free_url_state(cl); uclient_http_free_ssl_ctx(uh); + uh->ssl_ops = ops; uh->ssl_ctx = ctx; - uh->ssl_ctx_ext = !!ctx; uh->ssl_require_validation = !!ctx && require_validation; return 0; } +static void uclient_http_request_disconnect(struct uclient *cl) +{ + struct uclient_http *uh = container_of(cl, struct uclient_http, uc); + + if (!uh->us) + return; + + uh->eof = true; + uh->disconnect = true; + uloop_timeout_set(&uh->disconnect_t, 1); +} + const struct uclient_backend uclient_backend_http = { .prefix = uclient_http_prefix, .alloc = uclient_http_alloc, .free = uclient_http_free, .connect = uclient_http_connect, + .disconnect = uclient_http_request_disconnect, .update_url = uclient_http_free_url_state, .read = uclient_http_read,