Annotation of parser3/src/main/pa_http.C, revision 1.11
1.1 paf 1: /** @file
2: Parser: http support functions.
3:
4: Copyright(c) 2001-2005 ArtLebedev Group (http://www.artlebedev.com)
5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
6: */
7:
1.11 ! misha 8: static const char * const IDENT_HTTP_C="$Date: 2008-02-20 08:55:15 $";
1.1 paf 9:
10: #include "pa_http.h"
11: #include "pa_common.h"
12: #include "pa_charsets.h"
13: #include "pa_request_charsets.h"
14:
15: // defines
16:
17: #define HTTP_METHOD_NAME "method"
18: #define HTTP_FORM_NAME "form"
19: #define HTTP_BODY_NAME "body"
20: #define HTTP_TIMEOUT_NAME "timeout"
21: #define HTTP_HEADERS_NAME "headers"
1.10 misha 22: #define HTTP_COOKIES_NAME "cookies"
1.1 paf 23: #define HTTP_ANY_STATUS_NAME "any-status"
1.6 misha 24: // #define HTTP_CHARSET_NAME "charset"
1.1 paf 25: #define HTTP_TABLES_NAME "tables"
26: #define HTTP_USER "user"
27: #define HTTP_PASSWORD "password"
28:
29: #define DEFAULT_USER_AGENT "parser3"
30:
31: # ifndef INADDR_NONE
32: # define INADDR_NONE ((ulong) -1)
33: # endif
34:
35: #undef CRLF
36: #define CRLF "\r\n"
37:
38: static bool set_addr(struct sockaddr_in *addr, const char* host, const short port){
39: memset(addr, 0, sizeof(*addr));
40: addr->sin_family=AF_INET;
41: addr->sin_port=htons(port);
42: if(host) {
43: ulong packed_ip=inet_addr(host);
44: if(packed_ip!=INADDR_NONE)
45: memcpy(&addr->sin_addr, &packed_ip, sizeof(packed_ip));
46: else {
47: struct hostent *hostIP=gethostbyname(host);
48: if(hostIP)
49: memcpy(&addr->sin_addr, hostIP->h_addr, hostIP->h_length);
50: else
51: return false;
52: }
53: } else
54: addr->sin_addr.s_addr=INADDR_ANY;
55: return true;
56: }
57:
58: size_t guess_content_length(char* buf) {
59: char* ptr;
60: if((ptr=strstr(buf, "Content-Length:"))) // Apache
61: goto found;
62: if((ptr=strstr(buf, "content-length:"))) // Parser 3
63: goto found;
64: if((ptr=strstr(buf, "Content-length:"))) // maybe 1
65: goto found;
66: if((ptr=strstr(buf, "CONTENT-LENGTH:"))) // maybe 2
67: goto found;
68: return 0;
69: found:
70: char *error_pos;
71: size_t result=(size_t)strtol(ptr+15/*strlen("CONTENT-LENGTH:")*/, &error_pos, 0);
72:
73: const size_t reasonable_initial_max=0x400*0x400*10 /*10M*/;
74: if(result>reasonable_initial_max) // sanity check
75: return reasonable_initial_max;
76: return 0;//result;
77: }
78:
79: static int http_read_response(char*& response, size_t& response_size, int sock, bool fail_on_status_ne_200) {
80: int result=0;
81: // fetching some to local buffer, guessing on possible content-length
82: response_size=0x400*20; // initial size if content-length could not be determined
83: const size_t preview_size=0x400*20;
84: char preview_buf[preview_size+1/*terminator*/]; // 20K buffer to preview headers
85: ssize_t received_size=recv(sock, preview_buf, preview_size, 0);
86: if(received_size==0)
87: goto done;
88: if(received_size<0) {
89: if(int no=pa_socks_errno())
90: throw Exception("http.timeout",
91: 0,
92: "error receiving response header: %s (%d)", pa_socks_strerr(no), no);
93: goto done;
94: }
1.2 paf 95: // terminator [helps futher string searches]
96: preview_buf[received_size]=0;
97: // checking status
98: if(char* EOLat=strstr(preview_buf, "\n")) {
99: const String status_line(pa_strdup(preview_buf, EOLat-preview_buf));
100: ArrayString astatus;
101: size_t pos_after=0;
102: status_line.split(astatus, pos_after, " ");
103: const String& status_code=*astatus.get(astatus.count()>1?1:0);
104: result=status_code.as_int();
105:
106: if(fail_on_status_ne_200 && result!=200)
107: throw Exception("http.status",
108: &status_code,
109: "invalid HTTP response status");
110: }
1.1 paf 111: // detecting response_size
112: {
113: if(size_t content_length=guess_content_length(preview_buf))
114: response_size=preview_size+content_length; // a little more than needed, will adjust response_size by actual received size later
115: }
116:
117: // [gcc is happier this way, see goto above]
118: {
119: // allocating initial buf
120: response=(char*)pa_malloc_atomic(response_size+1/*terminator*/); // just setting memory block type
121: char* ptr=response;
122: size_t todo_size=response_size;
123: // coping part of already received body
124: memcpy(ptr, preview_buf, received_size);
125: ptr+=received_size;
126: todo_size-=received_size;
127:
128: // we use terminator byte for two purposes here:
129: // 1. we return there zero always, not knowing: maybe they would want to create String form $file.body?
130: // invariant: all Strings should have zero-terminated buffers
131: // 2. we use that out-of-size byte to detect if our content-length guess was wrong
132: // when recv gets more than we expected
133: // a) we know that the content-length guess was wrong
134: // b) we have space to put the first byte of extra data
135: // c) we use less code to detect normal situation: on last while-cycle recv expected to just return 0
136: while(true) {
137: received_size=recv(sock, ptr, todo_size+1/*there is always a place for terminator*/, 0);
138: if(received_size==0) {
139: response_size-=todo_size; // in case we received less than expected, cut down the reported size
140: break;
141: }
142: if(received_size<0) {
143: if(int no=pa_socks_errno())
144: throw Exception("http.timeout",
145: 0,
146: "error receiving response body: %s (%d)", pa_socks_strerr(no), no);
147: break;
148: }
149: // they've touched the terminator?
150: if((size_t)received_size>todo_size)
151: {
152: // that means that our guessed response_size was not big enough
153: const size_t grow_chunk_size=0x400*0x400; // 1M
154: response_size+=grow_chunk_size;
155: size_t ptr_offset=ptr-response;
156: response=(char*)pa_realloc(response, response_size+1/*terminator*/);
157: ptr=response+ptr_offset;
158: todo_size+=grow_chunk_size;
159: }
160: // can't do this before realloc: we need <todo_size check
161: ptr+=received_size;
162: todo_size-=received_size;
163: }
164: }
165: done:
166: if(result)
167: {
168: response[response_size]=0;
169: return result;
170: }
171: else
172: throw Exception("http.response",
173: 0,
174: "bad response from host - no status found (size=%u)", response_size);
175: }
176:
177: /* ********************** request *************************** */
178:
179: #if defined(SIGALRM) && defined(HAVE_SIGSETJMP) && defined(HAVE_SIGLONGJMP)
180: # define PA_USE_ALARM
181: #endif
182:
183: #ifdef PA_USE_ALARM
184: static sigjmp_buf timeout_env;
185: static void timeout_handler(int /*sig*/){
186: siglongjmp(timeout_env, 1);
187: }
188: #endif
189:
190: static int http_request(char*& response, size_t& response_size,
191: const char* host, short port,
192: const char* request,
193: int timeout_secs,
194: bool fail_on_status_ne_200) {
195: if(!host)
196: throw Exception("http.host",
197: 0,
198: "zero hostname"); //never
199:
200: volatile // to prevent makeing it register variable, because it will be clobbered by longjmp [thanks gcc warning]
201: int sock=-1;
202: #ifdef PA_USE_ALARM
203: signal(SIGALRM, timeout_handler);
204: #endif
205: #ifdef PA_USE_ALARM
206: if(sigsetjmp(timeout_env, 1)) {
207: // stupid gcc [2.95.4] generated bad code
208: // which failed to handle sigsetjmp+throw: crashed inside of pre-throw code.
209: // rewritten simplier [athough duplicating closesocket code]
210: if(sock>=0)
211: closesocket(sock);
212: throw Exception("http.timeout",
213: 0,
214: "timeout occured while retrieving document");
215: return 0; // never
216: } else {
217: alarm(timeout_secs);
218: #endif
219: try {
220: int result;
221: struct sockaddr_in dest;
222:
223: if(!set_addr(&dest, host, port))
224: throw Exception("http.host",
225: 0,
226: "can not resolve hostname \"%s\"", host);
227:
228: if((sock=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP/*0*/))<0) {
229: int no=pa_socks_errno();
230: throw Exception("http.connect",
231: 0,
232: "can not make socket: %s (%d)", pa_socks_strerr(no), no);
233: }
234:
235: // To enable SO_DONTLINGER (that is, disable SO_LINGER)
236: // l_onoff should be set to zero and setsockopt should be called
237: linger dont_linger={0,0};
238: setsockopt(sock, SOL_SOCKET, SO_LINGER, (const char *)&dont_linger, sizeof(dont_linger));
239:
240: #ifdef WIN32
241: // SO_*TIMEO can be defined in .h but not implemlemented in protocol,
242: // failing subsequently with Option not supported by protocol (99) message
243: // could not suppress that, so leaving this only for win32
244: int timeout_ms=timeout_secs*1000;
245: setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (const char*)&timeout_ms, sizeof(timeout_ms));
246: setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout_ms, sizeof(timeout_ms));
247: #endif
248:
249: if(connect(sock, (struct sockaddr *)&dest, sizeof(dest))) {
250: int no=pa_socks_errno();
251: throw Exception("http.connect",
252: 0,
253: "can not connect to host \"%s\": %s (%d)", host, pa_socks_strerr(no), no);
254: }
255: size_t request_size=strlen(request);
256: if(send(sock, request, request_size, 0)!=(ssize_t)request_size) {
257: int no=pa_socks_errno();
258: throw Exception("http.timeout",
259: 0,
260: "error sending request: %s (%d)", pa_socks_strerr(no), no);
261: }
262:
263: result=http_read_response(response, response_size, sock, fail_on_status_ne_200);
264: closesocket(sock);
265: #ifdef PA_USE_ALARM
266: alarm(0);
267: #endif
268: return result;
269: } catch(...) {
270: #ifdef PA_USE_ALARM
271: alarm(0);
272: #endif
273: if(sock>=0)
274: closesocket(sock);
275: rethrow;
276: }
277: #ifdef PA_USE_ALARM
278: }
279: #endif
280: }
281:
282: #ifndef DOXYGEN
283: struct Http_pass_header_info {
284: Request_charsets* charsets;
285: String* request;
286: bool user_agent_specified;
287: };
288: #endif
1.9 misha 289: static void http_pass_header(HashStringValue::key_type name,
1.1 paf 290: HashStringValue::value_type value,
291: Http_pass_header_info *info) {
1.9 misha 292:
1.10 misha 293: String aname=String(name, String::L_URI);
1.9 misha 294:
295: *info->request <<aname<<": "
1.10 misha 296: << attributed_meaning_to_string(*value, String::L_URI, false)
1.9 misha 297: << CRLF;
1.1 paf 298:
1.9 misha 299: if(aname.change_case(info->charsets->source(), String::CC_UPPER)=="USER-AGENT")
300: info->user_agent_specified=true;
1.1 paf 301: }
302:
1.10 misha 303: static void http_pass_cookie(HashStringValue::key_type name,
304: HashStringValue::value_type value,
305: Http_pass_header_info *info) {
306:
307: *info->request << String(name, String::L_HTTP_HEADER) << "="
308: << attributed_meaning_to_string(*value, String::L_HTTP_HEADER, false)
309: << "; ";
310:
311: }
1.1 paf 312:
313: static Charset* detect_charset(Charset& source_charset, const String& content_type_value) {
314: const String::Body CONTENT_TYPE_VALUE=
315: content_type_value.change_case(source_charset, String::CC_UPPER);
316: // content-type: xxx/xxx; source_charset=WE-NEED-THIS
317: // content-type: xxx/xxx; source_charset="WE-NEED-THIS"
318: // content-type: xxx/xxx; source_charset="WE-NEED-THIS";
319: size_t before_charseteq_pos=CONTENT_TYPE_VALUE.pos("CHARSET=");
320: if(before_charseteq_pos!=STRING_NOT_FOUND) {
1.5 misha 321: size_t charset_begin=before_charseteq_pos+8/*CHARSET=*/;
1.1 paf 322: size_t open_quote_pos=CONTENT_TYPE_VALUE.pos('"', charset_begin);
323: bool quoted=open_quote_pos==charset_begin;
324: if(quoted)
325: charset_begin++; // skip opening '"'
326: size_t charset_end=CONTENT_TYPE_VALUE.length();
327: if(quoted) {
328: size_t close_quote_pos=CONTENT_TYPE_VALUE.pos('"', charset_begin);
329: if(close_quote_pos!=STRING_NOT_FOUND)
330: charset_end=close_quote_pos;
331: } else {
332: size_t delim_pos=CONTENT_TYPE_VALUE.pos(';', charset_begin);
333: if(delim_pos!=STRING_NOT_FOUND)
334: charset_end=delim_pos;
335: }
336: const String::Body CHARSET_NAME_BODY=
1.4 misha 337: CONTENT_TYPE_VALUE.mid(charset_begin, charset_end - charset_begin);
1.1 paf 338:
339: return &charsets.get(CHARSET_NAME_BODY);
340: }
341:
342: return 0;
343: }
344:
345: static const String* basic_authorization_field(const char* user, const char* pass) {
346: if(!user&& !pass)
347: return 0;
348:
349: String combined;
350: if(user)
351: combined<<user;
352: combined<<":";
353: if(pass)
354: combined<<pass;
355:
356: String* result=new String("Basic "); *result<<pa_base64_encode(combined.cstr(), combined.length());
357: return result;
358: }
359:
360: static void form_string_value2string(
361: HashStringValue::key_type key,
362: const String& value,
363: String& result)
364: {
365: result << String(key, String::L_URI) << "=";
366: result.append(value, String::L_URI, true);
367: result<< "&";
368: }
369: #ifndef DOXYGEN
370: struct Form_table_value2string_info {
371: HashStringValue::key_type key;
372: String& result;
373:
374: Form_table_value2string_info(HashStringValue::key_type akey, String& aresult):
375: key(akey), result(aresult) {}
376: };
377: #endif
378: static void form_table_value2string(Table::element_type row, Form_table_value2string_info* info) {
379: form_string_value2string(info->key, *row->get(0), info->result);
380: }
381: static void form_value2string(
382: HashStringValue::key_type key,
383: HashStringValue::value_type value,
384: String* result)
385: {
386: if(const String* svalue=value->get_string())
387: form_string_value2string(key, *svalue, *result);
388: else if(Table* tvalue=value->get_table()) {
389: Form_table_value2string_info info(key, *result);
390: tvalue->for_each(form_table_value2string, &info);
391: } else
392: throw Exception(0,
393: new String(key, String::L_TAINTED),
394: "is %s, "HTTP_FORM_NAME" option value must either string or table", value->type());
395: }
1.5 misha 396: const char* pa_form2string(HashStringValue& form, Request_charsets& charsets) {
1.1 paf 397: String string;
1.3 paf 398: form.for_each<String*>(form_value2string, &string);
1.5 misha 399: return string.cstr(String::L_UNSPECIFIED, 0, &charsets);
1.1 paf 400: }
401: static void find_headers_end(char* p,
402: char*& headers_end_at,
403: char*& raw_body)
404: {
405: raw_body=p;
406: // \n\n
407: // \r\n\r\n
408: while((p=strchr(p, '\n'))) {
409: headers_end_at=++p; // \n>.<
410: if(*p=='\r') // \r\n>\r?<\n
411: p++;
412: if(*p=='\n') { // \r\n\r>\n?<
413: raw_body=p+1;
414: return;
415: }
416: }
417: headers_end_at=0;
418: }
419:
420: /// @todo build .cookies field. use ^file.tables.SET-COOKIES.menu{ for now
421: File_read_http_result pa_internal_file_read_http(Request_charsets& charsets,
422: const String& file_spec,
423: bool as_text,
424: HashStringValue *options) {
425: File_read_http_result result;
426: char host[MAX_STRING];
427: const char* uri;
428: short port;
1.10 misha 429: const char* method="GET";
1.1 paf 430: HashStringValue* form=0;
431: const char* body_cstr=0;
432: int timeout_secs=2;
433: bool fail_on_status_ne_200=true;
434: Value* vheaders=0;
1.10 misha 435: Value* vcookies=0;
1.11 ! misha 436: Value* vbody=0;
1.1 paf 437: Charset *asked_remote_charset=0;
438: const char* user_cstr=0;
439: const char* password_cstr=0;
440:
441: if(options) {
442: int valid_options=pa_get_valid_file_options_count(*options);
443:
444: if(Value* vmethod=options->get(HTTP_METHOD_NAME)) {
445: valid_options++;
446: method=vmethod->as_string().cstr();
447: }
448: if(Value* vform=options->get(HTTP_FORM_NAME)) {
449: valid_options++;
450: form=vform->get_hash();
451: }
1.11 ! misha 452: if(vbody=options->get(HTTP_BODY_NAME)) {
1.1 paf 453: valid_options++;
454: }
455: if(Value* vtimeout=options->get(HTTP_TIMEOUT_NAME)) {
456: valid_options++;
457: timeout_secs=vtimeout->as_int();
458: }
1.11 ! misha 459: if(vheaders=options->get(HTTP_HEADERS_NAME)) {
1.1 paf 460: valid_options++;
461: }
1.11 ! misha 462: if(vcookies=options->get(HTTP_COOKIES_NAME)) {
1.10 misha 463: valid_options++;
464: }
1.1 paf 465: if(Value* vany_status=options->get(HTTP_ANY_STATUS_NAME)) {
466: valid_options++;
467: fail_on_status_ne_200=!vany_status->as_bool();
468: }
1.6 misha 469: if(Value* vcharset_name=options->get(PA_CHARSET_NAME)) {
1.1 paf 470: asked_remote_charset=&::charsets.get(vcharset_name->as_string().
471: change_case(charsets.source(), String::CC_UPPER));
472: }
473: if(Value* vuser=options->get(HTTP_USER)) {
474: valid_options++;
475: user_cstr=vuser->as_string().cstr();
476: }
477: if(Value* vpassword=options->get(HTTP_PASSWORD)) {
478: valid_options++;
479: password_cstr=vpassword->as_string().cstr();
480: }
481:
482: if(valid_options!=options->count())
1.7 misha 483: throw Exception(PARSER_RUNTIME,
1.1 paf 484: 0,
485: "invalid option passed");
486: }
487: if(!asked_remote_charset) // defaulting to $request:charset
488: asked_remote_charset=&charsets.source();
489:
1.10 misha 490: bool method_is_get=strcmp(method, "GET")==0;
1.11 ! misha 491: if(vbody){
! 492: if(method_is_get)
! 493: throw Exception(PARSER_RUNTIME,
! 494: 0,
! 495: "you can not use $."HTTP_BODY_NAME" option with method GET");
! 496:
! 497: if(form)
! 498: throw Exception(PARSER_RUNTIME,
! 499: 0,
! 500: "you can not use options $."HTTP_BODY_NAME" and $."HTTP_FORM_NAME" together");
! 501: }
1.1 paf 502:
503: //preparing request
504: String& connect_string=*new String;
505: // not in ^sql{... L_SQL ...} spirit, but closer to ^file::load one
506: connect_string.append(file_spec, String::L_URI); // tainted pieces -> URI pieces
507:
508: String request_head_and_body;
509: {
510: // influence URLencoding of tainted pieces to String::L_URI lang
511: Temp_client_charset temp(charsets, *asked_remote_charset);
512:
1.5 misha 513: const char* connect_string_cstr=connect_string.cstr(String::L_UNSPECIFIED, 0, &charsets);
1.1 paf 514:
515: const char* current=connect_string_cstr;
516: if(strncmp(current, "http://", 7)!=0)
517: throw Exception(0,
518: &connect_string,
519: "does not start with http://"); //never
520: current+=7;
521:
522: strncpy(host, current, sizeof(host)-1); host[sizeof(host)-1]=0;
523: char* host_uri=lsplit(host, '/');
524: uri=host_uri?current+(host_uri-1-host):"/";
525: char* port_cstr=lsplit(host, ':');
526: char* error_pos=0;
527: port=port_cstr?(short)strtol(port_cstr, &error_pos, 0):80;
528:
529: bool uri_has_query_string=strchr(uri, '?')!=0;
530:
1.11 ! misha 531: // making request head
1.1 paf 532: String head;
1.11 ! misha 533: head << method << " " << uri;
! 534: if(form && method_is_get)
! 535: head << (uri_has_query_string?"&":"?") << pa_form2string(*form, charsets);
! 536:
! 537: head <<" HTTP/1.0" CRLF "host: "<< host << CRLF;
! 538:
1.1 paf 539: if(form && !method_is_get) {
540: head << "content-type: application/x-www-form-urlencoded" CRLF;
1.11 ! misha 541: body_cstr=pa_form2string(*form, charsets);
! 542: } else if (vbody) {
! 543: body_cstr=vbody->as_string().cstr(String::L_UNSPECIFIED, 0, &charsets);
! 544: // needed for transcoded $.body[] first of all
! 545: body_cstr=Charset::transcode(
! 546: String::C(body_cstr, strlen(body_cstr)),
! 547: charsets.source(),
! 548: *asked_remote_charset
! 549: );
1.1 paf 550: }
551:
552: // http://www.ietf.org/rfc/rfc2617.txt
553: if(const String* authorization_field_value=basic_authorization_field(user_cstr, password_cstr))
554: head<<"authorization: "<<*authorization_field_value<<CRLF;
555:
556: bool user_agent_specified=false;
557: if(vheaders && !vheaders->is_string()) { // allow empty
558: if(HashStringValue *headers=vheaders->get_hash()) {
559: Http_pass_header_info info={&charsets, &head, false};
1.3 paf 560: headers->for_each<Http_pass_header_info*>(http_pass_header, &info);
1.1 paf 561: user_agent_specified=info.user_agent_specified;
562: } else
1.7 misha 563: throw Exception(PARSER_RUNTIME,
1.1 paf 564: &connect_string,
565: "headers param must be hash");
566: };
567: if(!user_agent_specified) // defaulting
568: head << "user-agent: " DEFAULT_USER_AGENT CRLF;
569:
1.11 ! misha 570: if(vcookies && !vcookies->is_string()){ // allow empty
1.10 misha 571: if(HashStringValue* cookies=vcookies->get_hash()) {
572: head << "cookie: ";
573: Http_pass_header_info info={&charsets, &head, false};
574: cookies->for_each<Http_pass_header_info*>(http_pass_cookie, &info);
575: head << CRLF;
576: } else
577: throw Exception(PARSER_RUNTIME,
578: &connect_string,
579: "cookies param must be hash");
580: }
581:
1.1 paf 582: if(body_cstr) {
583: head << "content-length: " << format(strlen(body_cstr), "%u") << CRLF;
584: }
585:
1.6 misha 586: const char* head_cstr=head.cstr(String::L_UNSPECIFIED, 0, &charsets);
1.1 paf 587:
588: // head + end of header
589: request_head_and_body << head_cstr << CRLF;
1.8 misha 590:
1.1 paf 591: // body
592: if(body_cstr)
593: request_head_and_body << body_cstr;
594: }
595:
596: //sending request
597: char* response;
598: size_t response_size;
599: int status_code=http_request(response, response_size,
600: host, port, request_head_and_body.cstr(),
601: timeout_secs, fail_on_status_ne_200);
602:
603: //processing results
604: char* raw_body; size_t raw_body_size;
605: char* headers_end_at;
606: find_headers_end(response,
607: headers_end_at,
608: raw_body);
609: raw_body_size=response_size-(raw_body-response);
610:
611: result.headers=new HashStringValue;
612: VHash* vtables=new VHash;
613: result.headers->put(HTTP_TABLES_NAME, vtables);
614: Charset* real_remote_charset=0; // undetected, yet
615:
616: if(headers_end_at) {
617: *headers_end_at=0;
618: const String header_block(String::C(response, headers_end_at-response), true);
619:
620: ArrayString aheaders;
621: HashStringValue& tables=vtables->hash();
622:
623: size_t pos_after=0;
624: header_block.split(aheaders, pos_after, "\n");
625:
626: //processing headers
627: size_t aheaders_count=aheaders.count();
628: for(size_t i=1; i<aheaders_count; i++) {
629: const String& line=*aheaders.get(i);
630: size_t pos=line.pos(':');
631: if(pos==STRING_NOT_FOUND || pos<1)
632: throw Exception("http.response",
633: &connect_string,
634: "bad response from host - bad header \"%s\"", line.cstr());
635: const String::Body HEADER_NAME=
636: line.mid(0, pos).change_case(charsets.source(), String::CC_UPPER);
637: const String& header_value=line.mid(pos+1, line.length()).trim(String::TRIM_BOTH, " \t\r");
638: if(as_text && HEADER_NAME=="CONTENT-TYPE")
639: real_remote_charset=detect_charset(charsets.source(), header_value);
640:
641: // tables
642: {
643: Value *valready=(Value *)tables.get(HEADER_NAME);
644: bool existed=valready!=0;
645: Table *table;
646: if(existed) {
647: // second+ appearence
648: table=valready->get_table();
649: } else {
650: // first appearence
651: Table::columns_type columns =new ArrayString(1);
652: *columns+=new String("value");
653: table=new Table(columns);
654: }
655: // this string becomes next row
656: ArrayString& row=*new ArrayString(1);
657: row+=&header_value;
658: *table+=&row;
659: // not existed before? add it
660: if(!existed)
661: tables.put(HEADER_NAME, new VTable(table));
662: }
663:
664: result.headers->put(HEADER_NAME, new VString(header_value));
665: }
666: }
667:
668: // output response
669: String::C real_body=String::C(raw_body, raw_body_size);
670: if(as_text && raw_body_size) { // must be checked because transcode returns CONST string in case length==0, which contradicts hacking few lines below
671: // defaulting to used-asked charset [it's never empty!]
672: if(!real_remote_charset)
673: real_remote_charset=asked_remote_charset;
674: real_body=Charset::transcode(real_body, *real_remote_charset, charsets.source());
675: }
676:
677: result.str=const_cast<char *>(real_body.str); // hacking a little
678: result.length=real_body.length;
679: result.headers->put(file_status_name, new VInt(status_code));
680: return result;
681: }
E-mail: