summaryrefslogtreecommitdiff
path: root/yhttpd/src/reqp.cpp
blob: df9de5dd092dfb151086e673f844b5aea99d7b3d (plain)
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#ifndef REQP_CPP
#define REQP_CPP

#include "reqp.h"
#include "tool/tool.h"

using namespace std;

#define HEADER HEADER1 HEADER2 HEADER3 HEADER4 HEADER9
#define STREAM HEADER5 HEADER6

const string reqp::s_http = HEADER;
const string reqp::s_http_stream = STREAM;
const string reqp::s_http_colength = HEADER7;
const string reqp::s_http_cotype = HEADER8;
const string reqp::s_http_cotype_add = HEADER8b;

reqp::reqp( )
{}

void
reqp::get_request_parameters( string s_parameters, map<string,string>& map_params )
{
  string s_tmp;
  unsigned i_pos, i_pos2;

  while( (i_pos = s_parameters.find("&")) != string::npos )
  {
    s_tmp = s_parameters.substr(0, i_pos );

    if ( (i_pos2 = s_tmp.find("=")) != string::npos )
      map_params[ s_tmp.substr(0, i_pos2) ] = tool::replace( s_tmp.substr( i_pos2+1 ), "\\AND", "&");

    s_parameters = s_parameters.substr( i_pos + 1 );
  }

  // Get the last request parameter, which does not have a "&" on the end!
  if( (i_pos = s_parameters.find("=")) != string::npos )
    map_params[ s_parameters.substr(0, i_pos) ] = s_parameters.substr( i_pos+1 );

  //map<string,string>::iterator iter;
  //for ( iter = map_params.begin(); iter != map_params.end(); ++iter )
  //cout << ">>>" << iter->first << "=" << iter->second << endl;
}

string
reqp::get_url( string s_req, map<string, string> &map_params, int& i_postpayloadoffset )
{
  unsigned i_pos, i_pos2;
  string s_vars( "" );
  string s_ret;
  int i_req;

  // GET request
  if ( s_req.find("GET") != string::npos)
  {
    // Be sure that the GET request has minimum length
    if ( s_req.length() > 5 )
    {
      // Get rid of "GET /"
      if ( (i_pos = s_req.find("\n")) == string::npos )
        i_pos = s_req.length() - 1;

      s_req = s_req.substr(5, i_pos - 5);

      // Get HTML site to be displayed
      if ( (i_pos = s_req.find("?")) == string::npos )
      {
        if ( (i_pos2 = s_req.find(" HTTP")) != string::npos )
          s_ret = url_decode( s_req.substr(0, i_pos2));
      }
      else
      {
        s_ret = url_decode( s_req.substr(0, i_pos) );

        // Get request parameters:
        if ( (i_pos2 = s_req.find(" HTTP")) != string::npos )
        {
          s_req = url_decode( s_req.substr(i_pos + 1, i_pos2 - i_pos - 1) );
          get_request_parameters( s_req, map_params );
        }
      }

    }
  }

  // POST request
  else
  {
    if ( (i_pos2 = s_req.find("HTTP")) != string::npos )
    {
      if (i_pos2 > 13)
      {
        s_ret = url_decode( s_req.substr(6,i_pos2-7) );

        //wrap::system_message(s_req);
        //wrap::system_message(string("data offset=") + tool::int2string(i_postpayloadoffset)); 
        i_pos = s_req.find("event=",i_postpayloadoffset );
        if(i_pos != string::npos)
        {
          get_request_parameters( url_decode( s_req.substr(i_pos) ), map_params);
        }
      }
    }

  }

#ifdef VERBOSE
  wrap::system_message( REQUEST + s_ret );
#endif

  if ( s_ret.empty() )
    s_ret = wrap::CONF->get_elem( "httpd.startsite" );

  else
    s_ret = remove_dots(s_ret);

  map_params["request"] = s_ret;

  return s_ret;
}

string
reqp::get_content_type(string &s_file)
{
  string s_ext(tool::get_extension( s_file ));

  if( s_ext == "" )
    s_ext = "default";

  return wrap::CONF->get_elem( "httpd.contenttypes." + s_ext );
}

void
reqp::parse_headers( string s_req, map<string,string> &map_params )
{
  int pos = s_req.find("\n");

  if (pos != string::npos)
  {
    map_params["QUERY_STRING"] = tool::trim(s_req.substr(0,pos-1));

    int pos2;
    do
    {
      string s_line( s_req.substr(0, pos) );
      pos2 = s_line.find(":");

      if (pos2 != string::npos && s_line.length() > pos2+1)
        map_params[ tool::trim(s_line.substr(0, pos2)) ] = tool::trim(s_line.substr(pos2+1));

      s_req = s_req.substr( s_line.size() + 1 );
      pos = s_req.find("\n");
    }
    while( pos != string::npos);
  } // if
}

int
reqp::htoi(string *p_str)
{
  int value, c;
  c = p_str->at(0);

  if( isupper(c) )
    c = tolower(c);

  value = (c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10) * 16;

  c = p_str->at(1);

  if( isupper(c) )
    c = tolower(c);

  value += c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10;

  return value;
}

string
reqp::url_decode( string s_url )
{
  string s_dest = "";
  int i_len = s_url.size();
  int i_prv = i_len - 2;

  char c;
  for( int i = 0; i < i_len; ++i)
  {
    c = s_url.at(i);
    if( c == '+' )
    {
      s_dest += " ";
    }
    else if (c == '%' && i < i_prv)
    {
      string s_tmp = s_url.substr(i+1, 2);
      c = (char) htoi(&s_tmp);
      s_dest += c;
      i += 2;
    }
    else
    {
      s_dest += c;
    }
  }

  return s_dest;
}

string
reqp::get_from_header( string s_req, string s_hdr )
{
  unsigned i_pos[2];
  if ( (i_pos[0] = s_req.find( s_hdr, 0 )) == string::npos )
    return "";

  if ( (i_pos[1] = s_req.find( "\n", i_pos[0]) ) == string::npos )
    return "";

  unsigned i_len = s_hdr.length();
  return s_req.substr( i_pos[0] + i_len, i_pos[1] - i_pos[0] - i_len - 1 );
}

string
reqp::parse( socketcontainer *p_sock, string s_req, map<string,string> &map_params, int &i_postpayloadoffset )
{

  // store all request informations in map_params. store the url in
  // map_params["request"].
  get_url( s_req, map_params, i_postpayloadoffset );

  parse_headers( s_req, map_params );
  string s_event( map_params["event"] );

  map_params["content-type"] = get_content_type( map_params["request"] );

  string s_rep( "" );


  if ( wrap::CONF->get_elem("httpd.enablecgi").compare("true") == 0 &&
       string::npos != map_params["request"].find(".cgi") )
  {
    s_rep.append( tool::shell_command(
                    wrap::CONF->get_elem("httpd.templatedir") + map_params["request"],
                    METH_RETSTRING ) );
  }
  else
  {
    // parse and get the requested html-template and also use
    // the values stored in map_params for %%KEY%% substituations.
    s_rep.append( wrap::HTML->parse( map_params ) );
  }

  // create the http header.

  string s_resp(s_http);
  if ( s_event.compare("stream") == 0 )
    s_resp.append( s_http_stream );

  s_resp.append( s_http_colength + tool::int2string(s_rep.size()) + "\r\n" +
                 s_http_cotype + map_params["content-type"] +
                 s_http_cotype_add + "\r\n" );

  s_resp.append(s_rep);


  // return the parsed html-template.
  return  s_resp;
}


string
reqp::remove_dots( string s_ret )
{
  // remove ".." from the request.
  unsigned i_pos;

  if ( (i_pos = s_ret.find( ".." )) != string::npos )
    return remove_dots(s_ret.substr(0, i_pos));

  return s_ret;
}

#endif