ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/proj/trunk/Search/HttpHandler.cpp
(Generate patch)

Comparing trunk/Search/HttpHandler.cpp (file contents):
Revision 18 by douglas, 2002-12-09T21:40:12-08:00 vs.
Revision 25 by douglas, 2002-12-22T23:32:58-08:00

# Line 62 | Line 62 | HttpHandler::HttpHandler()
62          }
63   #endif // _WIN32
64  
65        begin = 0;
65          length = 0;
66          chunked = false;
67   }
# Line 76 | Line 75 | HttpHandler::~HttpHandler()
75   #endif // _WIN32
76   }
77  
78 < bool HttpHandler::handle(URL &url, bool head)
78 > bool HttpHandler::handle(URL &url, const string referer, bool head)
79   {
80          bool answer = false;
81  
# Line 133 | Line 132 | bool HttpHandler::handle(URL &url, bool
132                  delete [] port;
133          }
134  
135 +        if (referer != "")
136 +        {
137 +                putline("Referer: " + referer);
138 +        }
139 +
140          putline("Connection: close");
141          putline();
142  
# Line 151 | Line 155 | bool HttpHandler::handle(URL &url, bool
155                  unsigned dot = line.find('.');
156                  unsigned space = line.find(' ');
157  
158 <                unsigned major = strtoul(line.substr(5, dot - 5).c_str(), 0, 0);
159 <                unsigned minor = strtoul(line.substr(dot + 1, space - dot - 1).c_str(), 0,
160 <                        0);
158 >                unsigned major = strtoul(line.substr(5, dot - 5).c_str(), 0, 10);
159 >                unsigned minor = strtoul(line.substr(dot + 1, space - dot - 1).c_str(),
160 >                        0, 10);
161  
162 <                if (major > 1 || minor < 1)
162 >                if (major > 1)
163                  {
164 <                        cerr << program << ": Potentially Incompatible Server: HTTP/" << major
165 <                                << "." << minor << "\n";
164 >                        cerr << program << ": Potentially Incompatible Server: HTTP/" <<
165 >                                major << "." << minor << "\n";
166  
167                          return answer;
168                  }
169  
170 <                response = code(strtoul(line.substr(space + 1).c_str(), 0, 0));
170 >                response = code(strtoul(line.substr(space + 1).c_str(), 0, 10));
171  
172                  if (response < ok) do line = getline(); while (line != "");
173          }
# Line 182 | Line 186 | bool HttpHandler::handle(URL &url, bool
186  
187                          while (isspace(value[0])) value.erase(0, 1);
188  
189 < //                      if (field =
189 >                        if (field == "Content-Type")
190 >                        {
191 >                                type = value;
192 >                        }
193 >                        else if (field == "Content-Length")
194 >                        {
195 >                                length = strtoul(value.c_str(), 0, 10);
196 >                        }
197 >                        else if (field == "Location")
198 >                        {
199 >                                location = value;
200 >                        }
201 >                        else if (field == "Transfer-Encoding")
202 >                        {
203 >                                chunked = value == "chunked";
204 >                        }
205                  }
206          }
207          while (line != "");
# Line 217 | Line 236 | bool HttpHandler::handle(URL &url, bool
236                  break;
237          }
238  
239 +        if (!head && answer) populate();
240 +
241          return answer;
242   }
243  
244   HttpHandler& HttpHandler::getline(string& line, char endline)
245   {
246 <        int end = page.find(endline, begin);
247 <        int newline = page.find('\n', begin);
246 >        unsigned end = page.find(endline);
247 >        unsigned newline = page.find('\n');
248  
249          if (newline < end || end == string::npos)
250          {
251                  end = newline;
252          }
253  
254 <        line = page.substr(begin, end - begin);
255 <
235 <        if (end == string::npos)
236 <        {
237 <                begin = end;
238 <        }
239 <        else
240 <        {
241 <                begin = end + 1;
242 <        }
254 >        line = page.substr(0, end);
255 >        page.erase(0, (end == string::npos ? end : end + 1));
256  
257          return *this;
258   }
259  
247 bool HttpHandler::good()
248 {
249        bool answer = true;
250
251        if (begin >= page.length())
252        {
253                answer = false;
254        }
255        else if (begin == string::npos)
256        {
257                answer = false;
258        }
259
260        return answer;
261 }
262
260   void HttpHandler::clear()
261   {
262          closesocket(http);
# Line 267 | Line 264 | void HttpHandler::clear()
264          type = "";
265          length = 0;
266          location = "";
270        begin = 0;
267          page = "";
268          chunked = false;
269   }
270  
271 + void HttpHandler::populate()
272 + {
273 +        if (!chunked)
274 +        {
275 +                unsigned left = length;
276 +
277 +                while (left > 0)
278 +                {
279 +                        memset(buffer, 0, BUFSIZ + 1);
280 +
281 +                        unsigned bytes = left > BUFSIZ ? BUFSIZ : left;
282 +                        unsigned received;
283 +
284 +                        if ((received = recv(http, buffer, bytes, 0)) == SOCKET_ERROR)
285 +                        {
286 +                                error(program + ": Recv");
287 +                                exit(1);
288 +                        }
289 +                        else if (received != bytes)
290 +                        {
291 +                                left -= received;
292 +                                page += buffer;
293 +
294 +                                memset(buffer, 0, BUFSIZ + 1);
295 +
296 +                                bytes -= received;
297 +                                if (recv(http, buffer, bytes, 0) == SOCKET_ERROR)
298 +                                {
299 +                                        error(program + ": Recv");
300 +                                        exit(1);
301 +                                }
302 +                        }
303 +
304 +                        page += buffer;
305 +                        left -= bytes;
306 +                }
307 +        }
308 +        else
309 +        {
310 +                unsigned chunk;
311 +
312 +                do
313 +                {
314 +                        chunk = strtoul(getline().c_str(), 0, 16);
315 +
316 +                        unsigned left = chunk;
317 +
318 +                        while (left > 0)
319 +                        {
320 +                                memset(buffer, 0, BUFSIZ + 1);
321 +
322 +                                unsigned bytes = left > BUFSIZ ? BUFSIZ : left;
323 +                                unsigned received;
324 +
325 +                                if ((received = recv(http, buffer, bytes, 0)) == SOCKET_ERROR)
326 +                                {
327 +                                        error(program + ": Recv");
328 +                                        exit(1);
329 +                                }
330 +                                else if (received != bytes)
331 +                                {
332 +                                        left -= received;
333 +                                        page += buffer;
334 +
335 +                                        memset(buffer, 0, BUFSIZ + 1);
336 +
337 +                                        bytes -= received;
338 +                                        if (recv(http, buffer, bytes, 0) == SOCKET_ERROR)
339 +                                        {
340 +                                                error(program + ": Recv");
341 +                                                exit(1);
342 +                                        }
343 +                                }
344 +
345 +                                page += buffer;
346 +                                left -= bytes;
347 +                        }
348 +
349 +                        getline();
350 +                        length += chunk;
351 +                }
352 +                while (chunk > 0);
353 +        }
354 +
355 +        for (unsigned index = 0; index < page.length(); index++)
356 +        {
357 +                if (page[index] == '\r' && (index + 1 < page.length()) ? page[index +
358 +                        1] == '\n' : false)
359 +                {
360 +                        page.erase(index, 1);
361 +                }
362 +                else if (page[index] == '\r')
363 +                {
364 +                        page[index] = '\n';
365 +                }
366 +        }
367 + }
368 +
369   void HttpHandler::putline(const string line)
370   {
371          sprintf(buffer, "%s\r\n", line.c_str());

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines