From 5148266e5fd8b0e0694de6d012de9d7ce1f50e63 Mon Sep 17 00:00:00 2001 From: cxl Date: Sun, 16 Jul 2017 17:40:24 +0000 Subject: [PATCH] Core: HttpRequest: WhenContent now only receives data of final OK content (not redirection or authorization texts or errors) git-svn-id: svn://ultimatepp.org/upp/trunk@11249 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/Core/Http.cpp | 12 ++++------ uppsrc/Core/Inet.h | 3 ++- uppsrc/Core/src.tpp/HttpRequest$en-us.tpp | 28 ++++++++++++++++------- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/uppsrc/Core/Http.cpp b/uppsrc/Core/Http.cpp index 566f0e3e7..5c7d40d21 100644 --- a/uppsrc/Core/Http.cpp +++ b/uppsrc/Core/Http.cpp @@ -57,7 +57,7 @@ void HttpRequest::Init() redirect_count = 0; retry_count = 0; gzip = false; - WhenContent = callback(this, &HttpRequest::ContentOut); + all_content = false; WhenAuthenticate = callback(this, &HttpRequest::ResolveDigestAuthentication); chunk = 4096; timeout = 120000; @@ -760,11 +760,6 @@ void HttpRequest::StartBody() } } -void HttpRequest::ContentOut(const void *ptr, int size) -{ - body.Cat((const char *)ptr, size); -} - void HttpRequest::Out(const void *ptr, int size) { LLOG("HTTP Out " << size); @@ -775,7 +770,10 @@ void HttpRequest::Out(const void *ptr, int size) phase = FAILED; return; } - WhenContent(ptr, size); + if(WhenContent && (status_code >= 200 && status_code < 300 || all_content)) + WhenContent(ptr, size); + else + body.Cat((const char *)ptr, size); } diff --git a/uppsrc/Core/Inet.h b/uppsrc/Core/Inet.h index a5f6c4b0a..fdab7348a 100644 --- a/uppsrc/Core/Inet.h +++ b/uppsrc/Core/Inet.h @@ -389,6 +389,7 @@ class HttpRequest : public TcpSocket { bool std_headers; bool hasurlvar; bool keep_alive; + bool all_content; String contenttype; String username; String password; @@ -438,7 +439,6 @@ class HttpRequest : public TcpSocket { void CopyCookies(); void HttpError(const char *s); - void ContentOut(const void *ptr, int size); void Out(const void *ptr, int size); String CalculateDigest(const String& authenticate) const; @@ -467,6 +467,7 @@ public: HttpRequest& MaxRetries(int n) { max_retries = n; return *this; } HttpRequest& RequestTimeout(int ms) { timeout = ms; return *this; } HttpRequest& ChunkSize(int n) { chunk = n; return *this; } + HttpRequest& AllContent(bool b = true) { all_content = b; return *this; } HttpRequest& Method(int m, const char *custom_name = NULL); HttpRequest& GET() { return Method(METHOD_GET); } diff --git a/uppsrc/Core/src.tpp/HttpRequest$en-us.tpp b/uppsrc/Core/src.tpp/HttpRequest$en-us.tpp index a6b52d836..e94b4a8d7 100644 --- a/uppsrc/Core/src.tpp/HttpRequest$en-us.tpp +++ b/uppsrc/Core/src.tpp/HttpRequest$en-us.tpp @@ -18,26 +18,32 @@ topic "HttpRequest"; [s3;%% &] [ {{10000F(128)G(128)@1 [s0;%% [* Public Member List]]}}&] [s3; &] -[s5;:HttpRequest`:`:WhenContent: [_^Callback2^ Callback2]<[@(0.0.255) const]_[@(0.0.255) vo -id]_`*, [@(0.0.255) int]>_[* WhenContent]&] -[s2;%% Defines consumer function for HTTP response content. Default -value is method inside HttpRequest that gathers the output content -in String that can be read using GetContent method.&] +[s5;:HttpRequest`:`:WhenContent: [_^Callback2^ Event]<[@(0.0.255) const]_[@(0.0.255) void]_ +`*, [@(0.0.255) int]>_[* WhenContent]&] +[s2;%% Defines consumer function for HTTP response content. If defined, +HttpRequest uses this output event instead of storing the output +content in String that can be read using GetContent method. Note +that only `"valid`" content, defined as content in response with +status code in the range 200 .. 299 is sent to WhenContent `- +this avoids problems with multiple requests because of redirection +or authorization. It is possible to change this behavior with +AllContent `- in that case client code is likely to use WhenStart +to separate responses to individual requests.&] [s3; &] [s4; &] -[s5;:HttpRequest`:`:WhenStart: [_^Callback^ Callback]_[* WhenStart]&] +[s5;:HttpRequest`:`:WhenStart: [_^Callback^ Event<>]_[* WhenStart]&] [s2;%% Invoked each time HttpRequest starts a new request attempt `- this includes redirections, authentization or retries on error. Gives chance to client software to restart processing, e.g. to delete partially downloaded file.&] [s3; &] [s4; &] -[s5;:HttpRequest`:`:WhenDo: [_^Callback^ Callback]_[* WhenDo]&] +[s5;:HttpRequest`:`:WhenDo: [_^Callback^ Event<>]_[* WhenDo]&] [s2;%% Invoked each time Do routine exits. Useful to show progres or allow abortions in GUI.&] [s3; &] [s4; &] -[s5;:Upp`:`:HttpRequest`:`:WhenAuthenticate: [_^Upp`:`:Gate^ Gate]_[* WhenAuthenticate]&] +[s5;:Upp`:`:HttpRequest`:`:WhenAuthenticate: [_^Upp`:`:Gate^ Gate<>]_[* WhenAuthenticate]&] [s2;%% Invoked when request returns 401 code (unauthorized). Callback should check returned headers and if possible, provide authorization header (through Authorization). In that case, it should return @@ -83,6 +89,12 @@ ize]([@(0.0.255) int]_[*@3 n])&] (default is 4096). Returns `*this.&] [s3;%% &] [s4; &] +[s5;:Upp`:`:HttpRequest`:`:AllContent`(bool`): [_^Upp`:`:HttpRequest^ HttpRequest][@(0.0.255) `& +]_[* AllContent]([@(0.0.255) bool]_[*@3 b]_`=_[@(0.0.255) true])&] +[s2;%% When active, WhenContent receives content from all responses, +even if they are just redirection or authorization texts.&] +[s3;%% &] +[s4; &] [s5;:HttpRequest`:`:Method`(int`,const char`*`): [_^HttpRequest^ HttpRequest][@(0.0.255) `& ]_[* Method]([@(0.0.255) int]_[*@3 m], [@(0.0.255) const]_[@(0.0.255) char]_`*[*@3 custom`_na me]_`=_NULL)&]