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 20 by douglas, 2002-12-10T14:04:39-08:00

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines