File: Http_Request.cpp

package info (click to toggle)
libzen 0.4.41-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,496 kB
  • sloc: cpp: 10,502; ansic: 3,590; sh: 274; makefile: 97
file content (245 lines) | stat: -rw-r--r-- 9,249 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*  Copyright (c) MediaArea.net SARL. All Rights Reserved.
 *
 *  Use of this source code is governed by a zlib-style license that can
 *  be found in the License.txt file in the root of the source tree.
 */

//---------------------------------------------------------------------------
#include "ZenLib/PreComp.h"
#ifdef __BORLANDC__
    #pragma hdrstop
#endif
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
#include "ZenLib/Conf_Internal.h"
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
#include "ZenLib/Format/Http/Http_Request.h"
#include "ZenLib/Format/Http/Http_Utils.h"
#include "ZenLib/Ztring.h"
using namespace std;
//---------------------------------------------------------------------------

namespace ZenLib
{

namespace Format
{

namespace Http
{

//***************************************************************************
// Constructor/Destructor
//***************************************************************************

//---------------------------------------------------------------------------
Request::Request()
{
    //Config
    Http=new ZenLib::Format::Http::Handler;
    IsCopy=false;
}

//---------------------------------------------------------------------------
Request::Request(const Request &Req)
{
    //Config
    Http=Req.Http;
    IsCopy=true;
}

//---------------------------------------------------------------------------
Request::~Request()
{
    //Config
    if (!IsCopy)
        delete Http; //Http=NULL
}

//***************************************************************************
//
//***************************************************************************

//---------------------------------------------------------------------------
bool Request::Http_Begin(std::istream &In, std::ostream &Out)
{
    //First line, "Method Path Norm"
    //-Method
    string Method;
    In>>Method;
         if (Method.size()==3 && Method[0]=='G' && Method[1]=='E' && Method[2]=='T')
        ;
    else if (Method.size()==4 && Method[0]=='P' && Method[1]=='O' && Method[2]=='S' && Method[3]=='T')
        ;
    else if (Method.size()==4 && Method[0]=='H' && Method[1]=='E' && Method[2]=='A' && Method[3]=='D')
        Http->HeadersOnly=true;
    else
    {
        Out << "HTTP/1.0 501\r\n";
        Out << "\r\n";
        return false; //Unknown request
    }
    //-Path
    In>>Http->Path;
    if (Http->Path.empty() || Http->Path[0]!='/')
    {
        Out << "HTTP/1.0 404\r\n";
        Out << "\r\n";
        return false; //Problem
    }
    if (Http->Path.size()>8096) //Browsers/Servers have a 2K-8K limit, we do same
    {
        Out << "HTTP/1.0 414\r\n";
        Out << "\r\n";
        return false; //Problem
    }
    if (Http->Path.find('%')!=string::npos)
        Http->Path=Format::Http::URL_Encoded_Decode(Http->Path);
    //-Norm
    string Line;
    getline(In, Line); //Drop the first line, no more needed

    //Headers
    do
    {
        //Getting the line
        getline(In, Line);
        if (!Line.empty() && Line[Line.size()-1]=='\r')
            Line.resize(Line.size()-1); //Remove the \r

        //Processing the line, form is "aaa: bbb"
        if (!Line.empty())
        {
            string::size_type Separator_Pos=Line.find(':');
            string::size_type Content_Begin=Line.find_first_not_of(' ', Separator_Pos+1);
            if (Separator_Pos!=string::npos && Content_Begin!=string::npos)
            {
                string Command=Line.substr(0, Separator_Pos);
                if (Command=="Cookie")
                {
                    string Cookie=Line.substr(Separator_Pos+1, string::npos);
                    while (!Cookie.empty())
                    {
                        string::size_type Cookie_Pos=Cookie.rfind(';');
                        if (Cookie_Pos==string::npos)
                            Cookie_Pos=(string::size_type)-1;
                        string Line2=Cookie.substr(Cookie_Pos+1, string::npos);
                        TrimLeft(Line2, ' ');
                        if (Cookie_Pos!=(string::size_type)-1)
                        {
                            Cookie.resize(Cookie_Pos);
                            TrimLeft(Line2, ' ');
                        }
                        else
                            Cookie.clear();

                        string::size_type Separator_Pos2=Line2.find('=');
                        if (Separator_Pos2!=string::npos)
                            Http->Request_Cookies[Line2.substr(0, Separator_Pos2)]=Format::Http::URL_Encoded_Decode(Line2.substr(Separator_Pos2+1, string::npos));
                    }
                }
                else
                    Http->Request_Headers[Command]=Line.substr(Content_Begin, string::npos);
            }
        }
    }
    while (!Line.empty());

    //Info
    if ((Method.size()==3 && Method[0]=='G' && Method[1]=='E' && Method[2]=='T')
     || (Method.size()==4 && Method[0]=='P' && Method[1]=='O' && Method[2]=='S' && Method[3]=='T'))
    {
        do
        {
            string Content;

            //Getting the line
            string::size_type Interogation_Pos=Http->Path.find('?');
            if (Interogation_Pos!=string::npos)
            {
                Content=Http->Path.substr(Interogation_Pos+1, string::npos);
                Http->Path.resize(Interogation_Pos);
            }

            if (Method.size()==4) //Only for POST
            {
                int64u Content_Lengh=0;
                std::map<std::string, std::string>::iterator Header_Content_Lengh_Element=Http->Request_Headers.find("Content-Length");
                if (Header_Content_Lengh_Element==Http->Request_Headers.end())
                    Header_Content_Lengh_Element=Http->Request_Headers.find("Content-length");
                if (Header_Content_Lengh_Element!=Http->Request_Headers.end())
                    #ifdef UNICODE
                        Content_Lengh=Ztring().From_UTF8(Header_Content_Lengh_Element->second).To_int64u();
                    #else
                        Content_Lengh=Ztring(Header_Content_Lengh_Element->second).To_int64u();
                    #endif
                if (Content_Lengh>1024) //Verifying there is no big element
                {
                    Out << "HTTP/1.0 501\r\n";
                    Out << "\r\n";
                    return false; //Malformed request
                }

                size_t Content_Size_Current=Content.size();
                if (Content_Size_Current)
                {
                    Content+='&';
                    Content_Size_Current++;
                }
                Content.resize(Content_Size_Current+(size_t)Content_Lengh+1);
                In.read(&Content[Content_Size_Current], (streamsize)Content_Lengh);
                Content[Content_Size_Current+(size_t)Content_Lengh]='\0';
            }

            //Processing the line, form is "aaa=bbb&..."
            while (!Content.empty())
            {
                string::size_type Content_Pos=Content.rfind('&');
                if (Content_Pos==string::npos)
                    Content_Pos=(string::size_type)-1;
                std::string Line2=Content.substr(Content_Pos+1, string::npos);
                if (Content_Pos!=(string::size_type)-1)
                    Content.resize(Content_Pos);
                else
                    Content.clear();

                string::size_type Separator_Pos=Line2.find('=');
                if (Separator_Pos!=string::npos)
                    Http->Request_Queries[Line2.substr(0, Separator_Pos)]=Format::Http::URL_Encoded_Decode(Line2.substr(Separator_Pos+1, string::npos));
            }
        }
        while (!Line.empty());
    }

    return true;
}

void Request::Http_End(std::ostream &Out)
{
    Out << "HTTP/1.0 "<< Http->Response_HTTP_Code << "\r\n";
    for (std::map<std::string, std::string>::iterator Temp=Http->Response_Headers.begin(); Temp!=Http->Response_Headers.end(); ++Temp)
        Out << Temp->first << ": " << Temp->second << "\r\n";
    Http->Response_Cookies.Create_Lines(Out);
    std::map<std::string, std::string>::iterator Content_Type_Element=Http->Response_Headers.find("Content-Type");
    if (Content_Type_Element!=Http->Response_Headers.end())
        Out << "Content-Type: "<< Content_Type_Element->second << "\r\n";
    else if (Http->Response_HTTP_Code==200)
    {
        if (!Http->Response_Body.empty() && Http->Response_Body[0]=='<')
            Out << "Content-Type: "<< "text/html; charset=utf-8" << "\r\n";
    }
    if (!Http->Response_Body.empty())
        Out << "Content-Length: " << Http->Response_Body.size() << "\r\n";
    Out << "\r\n";
    if (!Http->HeadersOnly && !Http->Response_Body.empty())
        Out << Http->Response_Body.c_str();
}

} //Namespace

} //Namespace

} //Namespace