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
  
286
  
287
  
288
  
289
  
290
  
291
  
292
  
293
  
294
  
295
  
296
  
297
  
298
  
299
  
300
  
301
  
302
  
303
  
304
  
305
  
306
  
307
  
308
  
309
  
310
  
311
  
312
  
313
  
314
  
315
  
316
  
317
  
318
  
319
  
320
  
321
  
322
  
323
  
#pike __REAL_VERSION__ 
 
//! module Protocols 
//! submodule HTTP 
//! method object(Protocols.HTTP.Query) get_url(string|Standards.URI url) 
//! method object(Protocols.HTTP.Query) get_url(string|Standards.URI url,mapping query_variables) 
//! method object(Protocols.HTTP.Query) get_url(string|Standards.URI url,mapping query_variables,mapping request_headers) 
//! method object(Protocols.HTTP.Query) get_url(string|Standards.URI url,mapping query_variables,mapping request_headers,object(Protocols.HTTP.Query) query) 
//!   Sends a HTTP GET request to the server in the URL 
//!   and returns the created and initialized <ref>Query</ref> object. 
//!   0 is returned upon failure. If a query object having 
//!   request_headers->Connection=="Keep-Alive" from a previous request is 
//!   provided and the already established server connection can be used for 
//!   the next request, you may gain some performance. 
//! 
//! method object(Protocols.HTTP.Query) put_url(string|Standards.URI url) 
//! method object(Protocols.HTTP.Query) put_url(string|Standards.URI url,string file) 
//! method object(Protocols.HTTP.Query) put_url(string|Standards.URI url,string file,mapping query_variables) 
//! method object(Protocols.HTTP.Query) put_url(string|Standards.URI url,string file,mapping query_variables,mapping request_headers) 
//! method object(Protocols.HTTP.Query) put_url(string|Standards.URI url,string file,mapping query_variables,mapping request_headers,object(Protocols.HTTP.Query) query) 
//!   Sends a HTTP PUT request to the server in the URL 
//!   and returns the created and initialized <ref>Query</ref> object. 
//!   0 is returned upon failure. If a query object having 
//!   request_headers->Connection=="Keep-Alive" from a previous request is 
//!   provided and the already established server connection can be used for 
//!   the next request, you may gain some performance. 
//! 
//! method object(Protocols.HTTP.Query) delete_url(string|Standards.URI       url) 
//! method object(Protocols.HTTP.Query) delete_url(string|Standards.URI       url,mapping query_variables) 
//! method object(Protocols.HTTP.Query) delete_url(string|Standards.URI       url,mapping query_variables,mapping request_headers) 
//! method object(Protocols.HTTP.Query) delete_url(string|Standards.URI       url,mapping query_variables,mapping request_headers,object(Protocols.HTTP.Query) query) 
//!   Sends a HTTP DELETE request to the server in the URL 
//!   and returns the created and initialized <ref>Query</ref> object. 
//!   0 is returned upon failure. If a query object having 
//!   request_headers->Connection=="Keep-Alive" from a previous request is 
//!   provided and the already established server connection can be used for 
//!   the next request, you may gain some performance. 
//! 
//! method array(string) get_url_nice(string|Standards.URI url,mapping query_variables) 
//! method array(string) get_url_nice(string|Standards.URI url,mapping query_variables,mapping request_headers) 
//! method array(string) get_url_nice(string|Standards.URI url,mapping query_variables,mapping request_headers,object(Protocols.HTTP.Query) query) 
//! method string get_url_data(string|Standards.URI url,mapping query_variables) 
//! method string get_url_data(string|Standards.URI url,mapping query_variables,mapping request_headers) 
//! method string get_url_data(string|Standards.URI url,mapping query_variables,mapping request_headers,object(Protocols.HTTP.Query) query) 
//!   Returns an array of ({content_type,data}) and just the data 
//!   string respective,  
//!   after calling the requested server for the information. 
//!   0 is returned upon failure. 
//! 
//! 
//! method array(string) post_url_nice(string|Standards.URI url,mapping query_variables) 
//! method array(string) post_url_nice(string|Standards.URI url,mapping query_variables,mapping request_headers) 
//! method array(string) post_url_nice(string|Standards.URI url,mapping query_variables,mapping request_headers,object(Protocols.HTTP.Query) query) 
//! method string post_url_data(string|Standards.URI url,mapping query_variables) 
//! method string post_url_data(string|Standards.URI url,mapping query_variables,mapping request_headers) 
//! method string post_url_data(string|Standards.URI url,mapping query_variables,mapping request_headers,object(Protocols.HTTP.Query) query) 
//! method object(Protocols.HTTP.Query) post_url(string|Standards.URI url,mapping query_variables) 
//! method object(Protocols.HTTP.Query) post_url(string|Standards.URI url,mapping query_variables,mapping request_headers) 
//! method object(Protocols.HTTP.Query) post_url(string|Standards.URI url,mapping query_variables,mapping request_headers,object(Protocols.HTTP.Query) query) 
//!   Similar to the <ref>get_url</ref> class of functions, except that the 
//!   query variables is sent as a post request instead of a get. 
//! 
 
object do_method(string method, 
                 string|Standards.URI url, 
                 void|mapping query_variables, 
                 void|mapping request_headers, 
                 void|Protocols.HTTP.Query con, void|string data) 
{ 
  if(!con) 
    con = Protocols.HTTP.Query(); 
 
  if(stringp(url)) 
    url=Standards.URI(url); 
 
  if(url->scheme!="http") 
    error("Protocols.HTTP can't handle %O or any other protocol than HTTP\n", 
          url->scheme); 
 
  if(!request_headers) 
    request_headers = ([]); 
  mapping default_headers = ([ 
    "user-agent" : "Mozilla/4.0 compatible (Pike HTTP client)", 
    "host" : url->host ]); 
#if MIME.encode_base64 
  if(url->user || url->passwd) 
    default_headers->authorization = "Basic " 
                                   + MIME.encode_base64(url->user + ":" + 
                                                        (url->password || "")); 
#endif 
  request_headers = default_headers | request_headers; 
 
  string query=url->query; 
  if(query_variables && sizeof(query_variables)) 
  { 
    if(query) 
      query+="&"+http_encode_query(query_variables); 
    else 
      query=http_encode_query(query_variables); 
  } 
 
  string path=url->path; 
  if(path=="") path="/"; 
 
  con->sync_request(url->host,url->port, 
                    method+" "+path+(query?("?"+query):"")+" HTTP/1.0", 
                    request_headers, data); 
   
  if (!con->ok) return 0; 
  return con; 
} 
 
object get_url(string|Standards.URI url, 
               void|mapping query_variables, 
               void|mapping request_headers, 
               void|Protocols.HTTP.Query con) 
{ 
  return do_method("GET", url, query_variables, request_headers, con); 
} 
 
object put_url(string|Standards.URI url, 
               void|string file, 
               void|mapping query_variables, 
               void|mapping request_headers, 
               void|Protocols.HTTP.Query con) 
{ 
  return do_method("PUT", url, query_variables, request_headers, con); 
} 
 
object delete_url(string|Standards.URI url, 
                  void|mapping query_variables, 
                  void|mapping request_headers, 
                  void|Protocols.HTTP.Query con) 
{ 
  return do_method("DELETE", url, query_variables, request_headers, con); 
} 
 
array(string) get_url_nice(string|Standards.URI url, 
                           void|mapping query_variables, 
                           void|mapping request_headers, 
                           void|Protocols.HTTP.Query con) 
{ 
  object c = get_url(url, query_variables, request_headers, con); 
  return c && ({ c->headers["content-type"], c->data() }); 
} 
 
string get_url_data(string|Standards.URI url, 
                    void|mapping query_variables, 
                    void|mapping request_headers, 
                    void|Protocols.HTTP.Query con) 
{ 
  object z = get_url(url, query_variables, request_headers, con); 
  return z && z->data(); 
} 
 
object post_url(string|Standards.URI url, 
                mapping query_variables, 
                void|mapping request_headers, 
                void|Protocols.HTTP.Query con) 
{ 
  return do_method("POST", url, 0, 
                   (request_headers||([]))| 
                   (["content-type": 
                     "application/x-www-form-urlencoded"]), 
                   con, 
                   http_encode_query(query_variables)); 
} 
 
array(string) post_url_nice(string|Standards.URI url, 
                            mapping query_variables, 
                            void|mapping request_headers, 
                            void|Protocols.HTTP.Query con) 
{ 
  object c = post_url(url, query_variables, request_headers, con); 
  return c && ({ c->headers["content-type"], c->data() }); 
} 
 
string post_url_data(string|Standards.URI url, 
                     mapping query_variables, 
                     void|mapping request_headers, 
                     void|Protocols.HTTP.Query con) 
{ 
  object z = post_url(url, query_variables, request_headers, con); 
  return z && z->data(); 
} 
 
//! 
//! method string unentity(string s) 
//!   Helper function for replacing HTML entities 
//!   with the corresponding iso-8859-1 characters. 
//! note: 
//!   All characters isn't replaced, only those with 
//!   corresponding iso-8859-1 characters. 
 
string unentity(string s) 
{ 
   return replace( 
      s, 
 
      ({"&AElig;", "&Aacute;", "&Acirc;", "&Agrave;", "&Aring;", "&Atilde;", 
        "&Auml;", "&Ccedil;", "&ETH;", "&Eacute;", "&Ecirc;", "&Egrave;", 
        "&Euml;", "&Iacute;", "&Icirc;", "&Igrave;", "&Iuml;", "&Ntilde;", 
        "&Oacute;", "&Ocirc;", "&Ograve;", "&Oslash;", "&Otilde;", "&Ouml;", 
        "&THORN;", "&Uacute;", "&Ucirc;", "&Ugrave;", "&Uuml;", "&Yacute;", 
        "&aacute;", "&acirc;", "&aelig;", "&agrave;", "&apos;", "&aring;", 
        "&ast;", "&atilde;", "&auml;", "&brvbar;", "&ccedil;", "&cent;", 
        "&colon;", "&comma;", "&commat;", "&copy;", "&deg;", "&dollar;", 
        "&eacute;", "&ecirc;", "&egrave;", "&emsp;", "&ensp;", "&equals;", 
        "&eth;", "&euml;", "&excl;", "&frac12;", "&frac14;", "&frac34;", 
        "&frac18;", "&frac38;", "&frac58;", "&frac78;", "&gt;", "&gt", 
        "&half;", "&hyphen;", "&iacute;", "&icirc;", "&iexcl;", "&igrave;", 
        "&iquest;", "&iuml;", "&laquo;", "&lpar;", "&lsqb;", "&lt;", "&lt", 
        "&mdash;", "&micro;", "&middot;", "&nbsp;", "&ndash;", "&not;", 
        "&ntilde;", "&oacute;", "&ocirc;", "&ograve;", "&oslash;", "&otilde;", 
        "&ouml;", "&para;", "&percnt;", "&period;", "&plus;", "&plusmn;", 
        "&pound;", "&quest;", "&quot;", "&raquo;", "&reg;", "&rpar;", 
        "&rsqb;", "&sect;", "&semi;", "&shy;", "&sup1;", "&sup2;", "&sup3;", 
        "&szlig;", "&thorn;", "&tilde;", "&trade;", "&uacute;", "&ucirc;", 
        "&ugrave;", "&uuml;", "&yacute;", "&yen;", "&yuml;", "&verbar;", 
        "&amp;"}), 
 
      ({ "Æ", "Á", "Â", "À", "Å", "Ã", "Ä", "Ç", "Ð", "É", "Ê", "È", 
         "Ë", "Í", "Î", "Ì", "Ï", "Ñ", "Ó", "Ô", "Ò", "Ø", "Õ", "Ö", "Þ", "Ú", 
         "Û", "Ù", "Ü", "Ý", "á", "â", "æ", "à", "&apos;", "å", "&ast;", "ã", 
         "ä", "¦", "ç", "¢", ":", ",", "&commat;", "©", "°", 
         "$", "é", "ê", "è", "&emsp;", "&ensp;", "&equals;", "ð", "ë", 
         "!", "½", "¼", "¾", "&frac18;", "&frac38;", "&frac58;", 
         "&frac78;", ">", ">", "&half;", "&hyphen;", "í", "î", "¡", "ì", "¿", 
         "ï", "«", "(", "&lsqb;", "<", "<", "&mdash;", "µ", "·", "", 
         "&ndash;", "¬", "ñ", "ó", "ô", "ò", "ø", "õ", "ö", "¶", "%", 
         ".", "+", "±", "£", "?", "\"", "»", "®", ")", 
         "&rsqb;", "§", "&semi;", "­", "¹", "²", "³", "ß", "þ", "~", 
         "&trade;", "ú", "û", "ù", "ü", "ý", "¥", "ÿ", "&verbar;", "&", }), 
      ); 
} 
 
//! method string http_encode_query(mapping variables) 
//!   Encodes a query mapping to a string; 
//!   this protects odd - in http perspective - characters 
//!   like '&' and '#' and control characters, 
//!   and packs the result together in a HTTP query string. 
//! 
//!   Example: 
//!   <pre> 
//!   &gt; Protocols.HTTP.http_encode_query( (["anna":"eva","lilith":"blue"]) );   
//!     Result: "lilith=blue&anna=eva" 
//!     &gt; Protocols.HTTP.http_encode_query( (["&amp;":"&","'=\"":"\0\0\0"]) );   
//!     Result: "%26amp%3b=%26&%27%3d%22=%00%00%00" 
//!   </pre> 
 
 
string http_encode_query(mapping(string:int|string) variables) 
{ 
   return Array.map((array)variables, 
                    lambda(array(string|int) v) 
                    { 
                       if (intp(v[1])) 
                          return http_encode_string(v[0]); 
                       return http_encode_string(v[0])+"="+ 
                          http_encode_string(v[1]); 
                    })*"&"; 
} 
 
//! method string http_encode_string(string in) 
//!   This protects all odd - see <ref>http_encode_query</ref> -  
//!   characters for transfer in HTTP. 
//! 
//!   Do not use this function to protect URLs, since 
//!   it will protect URL characters like '/' and '?'. 
 
string http_encode_string(string f) 
{ 
   return replace( 
      f,  
      ({ "\000", "\001", "\002", "\003", "\004", "\005", "\006", "\007",  
         "\010", "\011", "\012", "\013", "\014", "\015", "\016", "\017",  
         "\020", "\021", "\022", "\023", "\024", "\025", "\026", "\027",  
         "\030", "\031", "\032", "\033", "\034", "\035", "\036", "\037",  
         "\200", "\201", "\202", "\203", "\204", "\205", "\206", "\207",  
         "\210", "\211", "\212", "\213", "\214", "\215", "\216", "\217",  
         "\220", "\221", "\222", "\223", "\224", "\225", "\226", "\227",  
         "\230", "\231", "\232", "\233", "\234", "\235", "\236", "\237",  
         " ", "%", "'", "\"", "+", "&", "=", "/",  
         "#", ";", "\\", "<", ">" }), 
      ({  
         "%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",     
         "%08", "%09", "%0a", "%0b", "%0c", "%0d", "%0e", "%0f",  
         "%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17",  
         "%18", "%19", "%1a", "%1b", "%1c", "%1d", "%1e", "%1f",  
         "%80", "%81", "%82", "%83", "%84", "%85", "%86", "%87",  
         "%88", "%89", "%8a", "%8b", "%8c", "%8d", "%8e", "%8f",  
         "%90", "%91", "%92", "%93", "%94", "%95", "%96", "%97",  
         "%98", "%99", "%9a", "%9b", "%9c", "%9d", "%9e", "%9f",  
         "%20", "%25", "%27", "%22", "%2b", "%26", "%3d", "%2f",  
         "%23", "%3b", "%5c", "%3c", "%3e"})); 
} 
 
string http_encode_cookie(string f) 
{ 
   return replace( 
      f,  
      ({ "\000", "\001", "\002", "\003", "\004", "\005", "\006", "\007",  
         "\010", "\011", "\012", "\013", "\014", "\015", "\016", "\017",  
         "\020", "\021", "\022", "\023", "\024", "\025", "\026", "\027",  
         "\030", "\031", "\032", "\033", "\034", "\035", "\036", "\037",  
         "\200", "\201", "\202", "\203", "\204", "\205", "\206", "\207",  
         "\210", "\211", "\212", "\213", "\214", "\215", "\216", "\217",  
         "\220", "\221", "\222", "\223", "\224", "\225", "\226", "\227",  
         "\230", "\231", "\232", "\233", "\234", "\235", "\236", "\237",  
         " ", "%", "'", "\"", ",", ";", "=" }), 
      ({  
         "%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",     
         "%08", "%09", "%0a", "%0b", "%0c", "%0d", "%0e", "%0f",  
         "%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17",  
         "%18", "%19", "%1a", "%1b", "%1c", "%1d", "%1e", "%1f",  
         "%80", "%81", "%82", "%83", "%84", "%85", "%86", "%87",  
         "%88", "%89", "%8a", "%8b", "%8c", "%8d", "%8e", "%8f",  
         "%90", "%91", "%92", "%93", "%94", "%95", "%96", "%97",  
         "%98", "%99", "%9a", "%9b", "%9c", "%9d", "%9e", "%9f",  
         "%20", "%25", "%27", "%22", "%2c", "%3b", "%3d" })); 
}