mirror of
https://github.com/sogou/workflow.git
synced 2026-02-08 01:33:17 +08:00
Merge pull request #779 from Barenboim/master
Make http client task more compatible to some non-standard responses
This commit is contained in:
@@ -336,15 +336,32 @@ bool ComplexHttpTask::need_redirect(ParsedURI& uri)
|
||||
|
||||
bool ComplexHttpTask::finish_once()
|
||||
{
|
||||
if (this->state != WFT_STATE_SUCCESS)
|
||||
{
|
||||
this->get_resp()->end_parsing();
|
||||
if (this->state == WFT_STATE_SYS_ERROR && this->error == ECONNRESET)
|
||||
{
|
||||
const http_parser_t *parser = this->get_resp()->get_parser();
|
||||
|
||||
/* Make it more compatible to some non-standard responses. */
|
||||
if (http_parser_header_complete(parser) &&
|
||||
!http_parser_keep_alive(parser) &&
|
||||
!http_parser_chunked(parser) &&
|
||||
!http_parser_has_content_length(parser))
|
||||
{
|
||||
this->state = WFT_STATE_SUCCESS;
|
||||
this->error = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (this->state == WFT_STATE_SUCCESS)
|
||||
{
|
||||
if (need_redirect(uri_))
|
||||
if (this->need_redirect(uri_))
|
||||
this->set_redirect(uri_);
|
||||
else if (this->state != WFT_STATE_SUCCESS)
|
||||
this->disable_retry();
|
||||
}
|
||||
else
|
||||
this->get_resp()->end_parsing();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -651,13 +651,13 @@ int http_parser_append_message(const void *buf, size_t *n,
|
||||
return 1;
|
||||
}
|
||||
|
||||
int http_parser_header_complete(http_parser_t *parser)
|
||||
int http_parser_header_complete(const http_parser_t *parser)
|
||||
{
|
||||
return parser->header_state == HPS_HEADER_COMPLETE;
|
||||
}
|
||||
|
||||
int http_parser_get_body(const void **body, size_t *size,
|
||||
http_parser_t *parser)
|
||||
const http_parser_t *parser)
|
||||
{
|
||||
if (parser->complete && parser->header_state == HPS_HEADER_COMPLETE)
|
||||
{
|
||||
|
||||
@@ -67,8 +67,8 @@ void http_parser_init(int is_resp, http_parser_t *parser);
|
||||
int http_parser_append_message(const void *buf, size_t *n,
|
||||
http_parser_t *parser);
|
||||
int http_parser_get_body(const void **body, size_t *size,
|
||||
http_parser_t *parser);
|
||||
int http_parser_header_complete(http_parser_t *parser);
|
||||
const http_parser_t *parser);
|
||||
int http_parser_header_complete(const http_parser_t *parser);
|
||||
int http_parser_set_method(const char *method, http_parser_t *parser);
|
||||
int http_parser_set_uri(const char *uri, http_parser_t *parser);
|
||||
int http_parser_set_version(const char *version, http_parser_t *parser);
|
||||
@@ -93,52 +93,52 @@ int http_header_cursor_find(const void *name, size_t name_len,
|
||||
}
|
||||
#endif
|
||||
|
||||
static inline const char *http_parser_get_method(http_parser_t *parser)
|
||||
static inline const char *http_parser_get_method(const http_parser_t *parser)
|
||||
{
|
||||
return parser->method;
|
||||
}
|
||||
|
||||
static inline const char *http_parser_get_uri(http_parser_t *parser)
|
||||
static inline const char *http_parser_get_uri(const http_parser_t *parser)
|
||||
{
|
||||
return parser->uri;
|
||||
}
|
||||
|
||||
static inline const char *http_parser_get_version(http_parser_t *parser)
|
||||
static inline const char *http_parser_get_version(const http_parser_t *parser)
|
||||
{
|
||||
return parser->version;
|
||||
}
|
||||
|
||||
static inline const char *http_parser_get_code(http_parser_t *parser)
|
||||
static inline const char *http_parser_get_code(const http_parser_t *parser)
|
||||
{
|
||||
return parser->code;
|
||||
}
|
||||
|
||||
static inline const char *http_parser_get_phrase(http_parser_t *parser)
|
||||
static inline const char *http_parser_get_phrase(const http_parser_t *parser)
|
||||
{
|
||||
return parser->phrase;
|
||||
}
|
||||
|
||||
static inline int http_parser_chunked(http_parser_t *parser)
|
||||
static inline int http_parser_chunked(const http_parser_t *parser)
|
||||
{
|
||||
return parser->chunked;
|
||||
}
|
||||
|
||||
static inline int http_parser_keep_alive(http_parser_t *parser)
|
||||
static inline int http_parser_keep_alive(const http_parser_t *parser)
|
||||
{
|
||||
return parser->keep_alive;
|
||||
}
|
||||
|
||||
static inline int http_parser_has_connection(http_parser_t *parser)
|
||||
static inline int http_parser_has_connection(const http_parser_t *parser)
|
||||
{
|
||||
return parser->has_connection;
|
||||
}
|
||||
|
||||
static inline int http_parser_has_content_length(http_parser_t *parser)
|
||||
static inline int http_parser_has_content_length(const http_parser_t *parser)
|
||||
{
|
||||
return parser->has_content_length;
|
||||
}
|
||||
|
||||
static inline int http_parser_has_keep_alive(http_parser_t *parser)
|
||||
static inline int http_parser_has_keep_alive(const http_parser_t *parser)
|
||||
{
|
||||
return parser->has_keep_alive;
|
||||
}
|
||||
|
||||
@@ -59,10 +59,6 @@ void http_callback(WFHttpTask *task)
|
||||
(tutorial_series_context *)series->get_context();
|
||||
auto *proxy_resp = context->proxy_task->get_resp();
|
||||
|
||||
/* Some servers may close the socket as the end of http response. */
|
||||
if (state == WFT_STATE_SYS_ERROR && error == ECONNRESET)
|
||||
state = WFT_STATE_SUCCESS;
|
||||
|
||||
if (state == WFT_STATE_SUCCESS)
|
||||
{
|
||||
const void *body;
|
||||
@@ -81,7 +77,6 @@ void http_callback(WFHttpTask *task)
|
||||
else
|
||||
{
|
||||
const char *err_string;
|
||||
int error = task->get_error();
|
||||
|
||||
if (state == WFT_STATE_SYS_ERROR)
|
||||
err_string = strerror(error);
|
||||
@@ -93,8 +88,7 @@ void http_callback(WFHttpTask *task)
|
||||
err_string = "URL error (Cannot be a HTTPS proxy)";
|
||||
|
||||
fprintf(stderr, "%s: Fetch failed. state = %d, error = %d: %s\n",
|
||||
context->url.c_str(), state, task->get_error(),
|
||||
err_string);
|
||||
context->url.c_str(), state, error, err_string);
|
||||
|
||||
/* As a tutorial, make it simple. And ignore reply status. */
|
||||
proxy_resp->set_status_code("404");
|
||||
|
||||
Reference in New Issue
Block a user