From 2808be58bd5f06ced951698bde80356993edd606 Mon Sep 17 00:00:00 2001 From: cxl Date: Tue, 22 May 2012 12:33:16 +0000 Subject: [PATCH] Core: Improved Http Cookie support git-svn-id: svn://ultimatepp.org/upp/trunk@4994 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/Core/Http.cpp | 9 ++-- uppsrc/Core/src.tpp/HttpCookie$en-us.tpp | 50 +++++++++++++++++++++++ uppsrc/Core/src.tpp/HttpRequest$en-us.tpp | 41 ++++++++++++++++--- uppsrc/Core/src.tpp/Inet$en-us.tpp | 16 ++++++++ 4 files changed, 108 insertions(+), 8 deletions(-) create mode 100644 uppsrc/Core/src.tpp/HttpCookie$en-us.tpp diff --git a/uppsrc/Core/Http.cpp b/uppsrc/Core/Http.cpp index fd718db66..0cdabdd15 100644 --- a/uppsrc/Core/Http.cpp +++ b/uppsrc/Core/Http.cpp @@ -470,6 +470,9 @@ void HttpRequest::StartRequest() data << path; } data << " HTTP/1.1\r\n"; + String pd = postdata; + if(method == METHOD_GET || method == METHOD_HEAD) + pd.Clear(); if(std_headers) { data << "URL: " << url << "\r\n" << "Host: " << host_port << "\r\n" @@ -477,8 +480,8 @@ void HttpRequest::StartRequest() << "Accept: " << Nvl(accept, "*/*") << "\r\n" << "Accept-Encoding: gzip\r\n" << "User-Agent: " << Nvl(agent, "U++ HTTP request") << "\r\n"; - if(postdata.GetCount()) - data << "Content-Length: " << postdata.GetCount() << "\r\n"; + if(pd.GetCount()) + data << "Content-Length: " << pd.GetCount() << "\r\n"; if(ctype.GetCount()) data << "Content-Type: " << ctype << "\r\n"; } @@ -500,7 +503,7 @@ void HttpRequest::StartRequest() else if(!force_digest && (!IsNull(username) || !IsNull(password))) data << "Authorization: Basic " << Base64Encode(username + ":" + password) << "\r\n"; - data << request_headers << "\r\n" << postdata; // !!! POST PHASE !!! + data << request_headers << "\r\n" << pd; LLOG("HTTP REQUEST " << host << ":" << port); LLOG("HTTP request:\n" << data); } diff --git a/uppsrc/Core/src.tpp/HttpCookie$en-us.tpp b/uppsrc/Core/src.tpp/HttpCookie$en-us.tpp new file mode 100644 index 000000000..197102397 --- /dev/null +++ b/uppsrc/Core/src.tpp/HttpCookie$en-us.tpp @@ -0,0 +1,50 @@ +topic "HttpCookie"; +[2 $$0,0#00000000000000000000000000000000:Default] +[i448;a25;kKO9;2 $$1,0#37138531426314131252341829483380:class] +[l288;2 $$2,2#27521748481378242620020725143825:desc] +[0 $$3,0#96390100711032703541132217272105:end] +[H6;0 $$4,0#05600065144404261032431302351956:begin] +[i448;a25;kKO9;2 $$5,0#37138531426314131252341829483370:item] +[l288;a4;*@5;1 $$6,6#70004532496200323422659154056402:requirement] +[l288;i1121;b17;O9;~~~.1408;2 $$7,0#10431211400427159095818037425705:param] +[i448;b42;O9;2 $$8,8#61672508125594000341940100500538:tparam] +[b42;2 $$9,9#13035079074754324216151401829390:normal] +[{_} +[ {{10000@(113.42.0) [s0;%% [*@7;4 HttpCookie]]}}&] +[s4; &] +[s1;:HttpCookie`:`:struct: [@(0.0.255)3 struct][3 _][*3 HttpCookie][3 _:_][@(0.0.255)3 public][3 _ +][*@3;3 Moveable][3 <][*3 HttpCookie][3 >_]&] +[s2;%% This simple structure is used to store information about HTTP +Cookie.&] +[s0;i448;a25;kKO9;:noref:@(0.0.255) &] +[ {{10000F(128)G(128)@1 [s0;%% [* Public Member List]]}}&] +[s3; &] +[s5;:HttpCookie`:`:id: [_^String^ String]_[* id]&] +[s2;%% The name of cookie.&] +[s3; &] +[s4; &] +[s5;:HttpCookie`:`:value: [_^String^ String]_[* value]&] +[s2;%% Value of cookie.&] +[s3; &] +[s4; &] +[s5;:HttpCookie`:`:domain: [_^String^ String]_[* domain]&] +[s2;%% Cookie domain.&] +[s3; &] +[s4; &] +[s5;:HttpCookie`:`:path: [_^String^ String]_[* path]&] +[s2;%% Cookie path.&] +[s3; &] +[s4; &] +[s5;:HttpCookie`:`:raw: [_^String^ String]_[* raw]&] +[s2;%% Unparsed cookie.&] +[s3; &] +[s4; &] +[s5;:HttpCookie`:`:Clear`(`): [@(0.0.255) void]_[* Clear]()&] +[s2;%% Clears all member variables.&] +[s3; &] +[s4; &] +[s5;:HttpCookie`:`:Parse`(const String`&`): [@(0.0.255) bool]_[* Parse]([@(0.0.255) const]_ +[_^String^ String][@(0.0.255) `&]_[*@3 cookie])&] +[s2;%% Parses [%-*@3 cookie] into member variables `- expects the format +of value of HTTP `"Set`-Cookie`" header.&] +[s3;%% ] \ No newline at end of file diff --git a/uppsrc/Core/src.tpp/HttpRequest$en-us.tpp b/uppsrc/Core/src.tpp/HttpRequest$en-us.tpp index 563f96672..41ae9cd48 100644 --- a/uppsrc/Core/src.tpp/HttpRequest$en-us.tpp +++ b/uppsrc/Core/src.tpp/HttpRequest$en-us.tpp @@ -175,6 +175,11 @@ ing][@(0.0.255) `&]_[*@3 data])&] [s2;%% Sets method to POST and name/value pair to the post data in the format used by HTML forms with `"POST`" method. Returns `*this.&] [s3;%% &] +[s4; &] +[s5;:HttpRequest`:`:ClearPost`(`): [_^HttpRequest^ HttpRequest][@(0.0.255) `&]_[* ClearPost +]()&] +[s2;%% Empties all Post data and sets the method to GET.&] +[s3; &] [s4;%% &] [s5;:HttpRequest`:`:Headers`(const String`&`): [_^HttpRequest^ HttpRequest][@(0.0.255) `& ]_[* Headers]([@(0.0.255) const]_[_^String^ String][@(0.0.255) `&]_[*@3 h])&] @@ -198,11 +203,27 @@ generated header fields. Returns `*this.&] [@(0.0.255) const]_[_^String^ String][@(0.0.255) `&]_[*@3 data])&] [s2;%% Adds single name/value pair to headers. Returns `*this.&] [s3;%% &] -[s4;%% &] -[s5;:HttpRequest`:`:Cookie`(const String`&`): [_^HttpRequest^ HttpRequest][@(0.0.255) `&]_ -[* Cookie]([@(0.0.255) const]_[_^String^ String][@(0.0.255) `&]_[*@3 cookie])&] -[s2;%% Adds cookie to HTTP request `- same as Header(`"Cookie`", -[%-*@3 cookie][%- )]. Returns `*this.&] +[s4; &] +[s5;:HttpRequest`:`:Cookie`(const HttpCookie`&`): [_^HttpRequest^ HttpRequest][@(0.0.255) `& +]_[* Cookie]([@(0.0.255) const]_[_^HttpCookie^ HttpCookie][@(0.0.255) `&]_[*@3 c])&] +[s2;%% Sets the cookie to be send with any subsequent request. Member +raw of HttpCookie is ignored. Cookie is only send when path and +domain match (can be set to Null to match always).&] +[s3;%% &] +[s4; &] +[s5;:HttpRequest`:`:Cookie`(const String`&`,const String`&`,const String`&`,const String`&`): [_^HttpRequest^ H +ttpRequest][@(0.0.255) `&]_[* Cookie]([@(0.0.255) const]_[_^String^ String][@(0.0.255) `&]_ +[*@3 id], [@(0.0.255) const]_[_^String^ String][@(0.0.255) `&]_[*@3 value], +[@(0.0.255) const]_[_^String^ String][@(0.0.255) `&]_[*@3 domain]_`=_Null, +[@(0.0.255) const]_[_^String^ String][@(0.0.255) `&]_[*@3 path]_`=_Null)&] +[s2;%% Sets the cookie to be send with any subsequent request. Member +raw of HttpCookie is ignored. Cookie is only send when path and +domain match (can be set to Null to match always)..&] +[s3;%% &] +[s4; &] +[s5;:HttpRequest`:`:CopyCookies`(const HttpRequest`&`): [_^HttpRequest^ HttpRequest][@(0.0.255) `& +]_[* CopyCookies]([@(0.0.255) const]_[_^HttpRequest^ HttpRequest][@(0.0.255) `&]_[*@3 r])&] +[s2;%% Copies cookies from another HttpRequest.&] [s3;%% &] [s4;%% &] [s5;:HttpRequest`:`:StdHeaders`(bool`): [_^HttpRequest^ HttpRequest][@(0.0.255) `&]_[* StdH @@ -321,6 +342,16 @@ onst]&] onst]&] [s2;%% If request is finished, returns HTTP reason phrase.&] [s3;%% &] +[s4; &] +[s5;:HttpRequest`:`:GetHttpHeader`(`)const: [@(0.0.255) const]_[_^HttpHeader^ HttpHeader][@(0.0.255) `& +]_[* GetHttpHeader]()_[@(0.0.255) const]&] +[s2;%% Returns HttpHeader of response.&] +[s3; &] +[s4; &] +[s5;:HttpRequest`:`:GetCookie`(const char`*`): [_^String^ String]_[* GetCookie]([@(0.0.255) c +onst]_[@(0.0.255) char]_`*[*@3 id])&] +[s2;%% Returns the value of cookie [%-*@3 id] of response.&] +[s3;%% &] [s4;%% &] [s5;:HttpRequest`:`:GetContent`(`)const: [_^String^ String]_[* GetContent]()_[@(0.0.255) co nst]&] diff --git a/uppsrc/Core/src.tpp/Inet$en-us.tpp b/uppsrc/Core/src.tpp/Inet$en-us.tpp index f26701038..347b90bdb 100644 --- a/uppsrc/Core/src.tpp/Inet$en-us.tpp +++ b/uppsrc/Core/src.tpp/Inet$en-us.tpp @@ -105,6 +105,12 @@ different for response and request.&] Note that the same key can be present multiple times.&] [s3; &] [s4; &] +[s5;:HttpHeader`:`:cookies: [_^VectorMap^ VectorMap]<[_^String^ String], +[_^HttpCookie^ HttpCookie]>_[* cookies]&] +[s2;%% Set of http response http cookies (filled based on Set`-Cookie +headers during parsing).&] +[s3; &] +[s4; &] [s5;:HttpHeader`:`:scgi: [@(0.0.255) bool]_[* scgi]&] [s2;%% Indicates that this header is a result of parsing scgi.&] [s3; &] @@ -115,6 +121,12 @@ Note that the same key can be present multiple times.&] be lower`-case) or empty string if such field is not present.&] [s3;%% &] [s4; &] +[s5;:HttpHeader`:`:GetCookie`(const char`*`)const: [_^String^ String]_[* GetCookie]([@(0.0.255) c +onst]_[@(0.0.255) char]_`*[*@3 id])_[@(0.0.255) const]&] +[s2;%% Returns the value of first cookie of response (set by Set`-Cookie) +with name [%-*@3 id].&] +[s3;%% &] +[s4; &] [s5;:HttpHeader`:`:Response`(String`&`,int`&`,String`&`)const: [@(0.0.255) bool]_[* Respo nse]([_^String^ String][@(0.0.255) `&]_[*@3 protocol], [@(0.0.255) int`&]_[*@3 code], [_^String^ String][@(0.0.255) `&]_[*@3 reason])_[@(0.0.255) const]&] @@ -189,4 +201,8 @@ was normal HTTP.&] SCGI requests based on first character of request `- if it is a digit, SCGI is assumed and the header is parsed as SCGI.&] [s3;%% &] +[s4; &] +[s5;:HttpHeader`:`:HttpHeader`(`): [* HttpHeader]()&] +[s2;%% Default constructor.&] +[s3; &] [s0;%% ] \ No newline at end of file