a580e12000-09-27Fredrik Hübinette (Hubbe) #pike __REAL_VERSION__
f4b01a1999-04-09Mirar (Pontus Hagland) 
b648852001-01-11Johan Schön 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) {
15c1182001-04-18Pär Svensson  if(!con) {
b648852001-01-11Johan Schön  con = Protocols.HTTP.Query();
15c1182001-04-18Pär Svensson  } if(!request_headers) request_headers = ([]);
b648852001-01-11Johan Schön  if(stringp(url)) url=Standards.URI(url);
15c1182001-04-18Pär Svensson  #if constant(SSL.sslfile) if(url->scheme!="http" && url->scheme!="https") error("Protocols.HTTP can't handle %O or any other protocols than HTTP or HTTPS\n", url->scheme); con->https= (url->scheme=="https")? 1 : 0; #else if(url->scheme!="http" )
b648852001-01-11Johan Schön  error("Protocols.HTTP can't handle %O or any other protocol than HTTP\n", url->scheme);
15c1182001-04-18Pär Svensson  #endif
b648852001-01-11Johan Schön 
85f9042001-01-31Johan Sundström  if(!request_headers) request_headers = ([]); mapping default_headers = ([
6bbe932003-01-19Martin Nilsson  "user-agent" : "Mozilla/5.0 (compatible; MSIE 6.0; Pike HTTP client)" " Pike/" + __REAL_MAJOR__ + "." + __REAL_MINOR__ + "." + __REAL_BUILD__,
85f9042001-01-31Johan Sundström  "host" : url->host ]);
78d9e82001-04-02Johan Sundström 
85f9042001-01-31Johan Sundström  if(url->user || url->passwd) default_headers->authorization = "Basic " + MIME.encode_base64(url->user + ":" + (url->password || "")); request_headers = default_headers | request_headers;
b648852001-01-11Johan Schön  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",
85f9042001-01-31Johan Sundström  request_headers, data);
b648852001-01-11Johan Schön  if (!con->ok) return 0; return con; }
7dc3162001-04-27Henrik Grubbström (Grubba) //! @decl Protocols.HTTP.Query get_url(string|Standards.URI url) //! @decl Protocols.HTTP.Query get_url(string|Standards.URI url, @ //! mapping query_variables) //! @decl Protocols.HTTP.Query get_url(string|Standards.URI url, @ //! mapping query_variables, @ //! mapping request_headers) //! @decl Protocols.HTTP.Query get_url(string|Standards.URI url, @ //! mapping query_variables, @ //! mapping request_headers, @ //! Protocols.HTTP.Query query) //! Sends a HTTP GET request to the server in the URL //! and returns the created and initialized @[Query] object. //! 0 is returned upon failure. If a query object having
cbe8c92003-04-07Martin Nilsson //! @expr{request_headers->Connection=="Keep-Alive"@} from a previous
7dc3162001-04-27Henrik Grubbström (Grubba) //! request is provided and the already established server connection //! can be used for the next request, you may gain some performance. //!
b648852001-01-11Johan Schön object get_url(string|Standards.URI url,
3f3b4e2000-08-04Johan Sundström  void|mapping query_variables,
fdfa092000-11-26Johan Sundström  void|mapping request_headers, void|Protocols.HTTP.Query con)
f4b01a1999-04-09Mirar (Pontus Hagland) {
b648852001-01-11Johan Schön  return do_method("GET", url, query_variables, request_headers, con);
f4b01a1999-04-09Mirar (Pontus Hagland) }
7dc3162001-04-27Henrik Grubbström (Grubba) //! @decl Protocols.HTTP.Query put_url(string|Standards.URI url) //! @decl Protocols.HTTP.Query put_url(string|Standards.URI url,string file) //! @decl Protocols.HTTP.Query put_url(string|Standards.URI url,string file, @ //! mapping query_variables) //! @decl Protocols.HTTP.Query put_url(string|Standards.URI url,string file, @ //! mapping query_variables, @ //! mapping request_headers) //! @decl Protocols.HTTP.Query put_url(string|Standards.URI url,string file, @ //! mapping query_variables, @ //! mapping request_headers, @ //! Protocols.HTTP.Query query) //! Sends a HTTP PUT request to the server in the URL //! and returns the created and initialized @[Query] object. //! 0 is returned upon failure. If a query object having
cbe8c92003-04-07Martin Nilsson //! @expr{request_headers->Connection=="Keep-Alive"@} from a previous
7dc3162001-04-27Henrik Grubbström (Grubba) //! request is provided and the already established server connection //! can be used for the next request, you may gain some performance. //!
b648852001-01-11Johan Schön object put_url(string|Standards.URI url,
3f3b4e2000-08-04Johan Sundström  void|string file, void|mapping query_variables,
fdfa092000-11-26Johan Sundström  void|mapping request_headers, void|Protocols.HTTP.Query con)
2ba20a2000-02-24Fredrik Noring {
4ff48d2002-11-08Anders Johansson  return do_method("PUT", url, query_variables, request_headers, con, file);
2ba20a2000-02-24Fredrik Noring }
7dc3162001-04-27Henrik Grubbström (Grubba) //! @decl Protocols.HTTP.Query delete_url(string|Standards.URI url) //! @decl Protocols.HTTP.Query delete_url(string|Standards.URI url, @ //! mapping query_variables) //! @decl Protocols.HTTP.Query delete_url(string|Standards.URI url, @ //! mapping query_variables, @ //! mapping request_headers) //! @decl Protocols.HTTP.Query delete_url(string|Standards.URI url, @ //! mapping query_variables, @ //! mapping request_headers, @ //! Protocols.HTTP.Query query) //! Sends a HTTP DELETE request to the server in the URL //! and returns the created and initialized @[Query] object. //! 0 is returned upon failure. If a query object having
cbe8c92003-04-07Martin Nilsson //! @expr{request_headers->Connection=="Keep-Alive"@} from a previous
7dc3162001-04-27Henrik Grubbström (Grubba) //! request is provided and the already established server connection //! can be used for the next request, you may gain some performance. //!
b648852001-01-11Johan Schön object delete_url(string|Standards.URI url,
3f3b4e2000-08-04Johan Sundström  void|mapping query_variables,
fdfa092000-11-26Johan Sundström  void|mapping request_headers, void|Protocols.HTTP.Query con)
2ba20a2000-02-24Fredrik Noring {
b648852001-01-11Johan Schön  return do_method("DELETE", url, query_variables, request_headers, con);
2ba20a2000-02-24Fredrik Noring }
7dc3162001-04-27Henrik Grubbström (Grubba) //! @decl array(string) get_url_nice(string|Standards.URI url, @ //! mapping query_variables) //! @decl array(string) get_url_nice(string|Standards.URI url, @ //! mapping query_variables, @ //! mapping request_headers) //! @decl array(string) get_url_nice(string|Standards.URI url, @ //! mapping query_variables, @ //! mapping request_headers, @ //! Protocols.HTTP.Query query) //! @decl string get_url_data(string|Standards.URI url, @ //! mapping query_variables) //! @decl string get_url_data(string|Standards.URI url, @ //! mapping query_variables, @ //! mapping request_headers) //! @decl string get_url_data(string|Standards.URI url, @ //! mapping query_variables, @ //! mapping request_headers, @ //! object(Protocols.HTTP.Query) query)
cbe8c92003-04-07Martin Nilsson //! Returns an array of @expr{({content_type,data})@} and just //! the data string respective,
7dc3162001-04-27Henrik Grubbström (Grubba) //! after calling the requested server for the information.
9eb4872003-11-25Martin Nilsson //! 0 is returned upon failure. Redirects (HTTP 302) are //! automatically followed.
7dc3162001-04-27Henrik Grubbström (Grubba) //!
b648852001-01-11Johan Schön array(string) get_url_nice(string|Standards.URI url,
fdfa092000-11-26Johan Sundström  void|mapping query_variables, void|mapping request_headers, void|Protocols.HTTP.Query con)
9a46751999-05-29Mirar (Pontus Hagland) {
9eb4872003-11-25Martin Nilsson  .Query c; multiset seen = (<>); do { if(!url) return 0; if(seen[url] || sizeof(seen)>1000) return 0; seen[url]=1; c = get_url(url, query_variables, request_headers, con); if(!c) return 0; if(c->status==302) url = c->headers->location; } while( c->status!=200 ); return ({ c->headers["content-type"], c->data() });
9a46751999-05-29Mirar (Pontus Hagland) }
b648852001-01-11Johan Schön string get_url_data(string|Standards.URI url,
fdfa092000-11-26Johan Sundström  void|mapping query_variables, void|mapping request_headers, void|Protocols.HTTP.Query con)
9a46751999-05-29Mirar (Pontus Hagland) {
9eb4872003-11-25Martin Nilsson  array(string) z = get_url_nice(url, query_variables, request_headers, con); return z && z[1];
9a46751999-05-29Mirar (Pontus Hagland) }
7dc3162001-04-27Henrik Grubbström (Grubba) //! @decl array(string) post_url_nice(string|Standards.URI url, @ //! mapping query_variables) //! @decl array(string) post_url_nice(string|Standards.URI url, @ //! mapping query_variables, @ //! mapping request_headers) //! @decl array(string) post_url_nice(string|Standards.URI url, @ //! mapping query_variables, @ //! mapping request_headers, @ //! Protocols.HTTP.Query query) //! @decl string post_url_data(string|Standards.URI url, @ //! mapping query_variables) //! @decl string post_url_data(string|Standards.URI url, @ //! mapping query_variables, @ //! mapping request_headers) //! @decl string post_url_data(string|Standards.URI url, @ //! mapping query_variables, @ //! mapping request_headers, @ //! Protocols.HTTP.Query query) //! @decl object(Protocols.HTTP.Query) post_url(string|Standards.URI url, @ //! mapping query_variables) //! @decl object(Protocols.HTTP.Query) post_url(string|Standards.URI url, @ //! mapping query_variables, @ //! mapping request_headers) //! @decl object(Protocols.HTTP.Query) post_url(string|Standards.URI url, @ //! mapping query_variables, @ //! mapping request_headers, @ //! Protocols.HTTP.Query query) //! Similar to the @[get_url()] class of functions, except that the //! query variables is sent as a POST request instead of as a GET. //!
b648852001-01-11Johan Schön object post_url(string|Standards.URI url,
fdfa092000-11-26Johan Sundström  mapping query_variables, void|mapping request_headers, void|Protocols.HTTP.Query con)
9a46751999-05-29Mirar (Pontus Hagland) {
b648852001-01-11Johan Schön  return do_method("POST", url, 0,
cb35ec2001-01-13Mirar (Pontus Hagland)  (request_headers||([]))|
b648852001-01-11Johan Schön  (["content-type": "application/x-www-form-urlencoded"]), con, http_encode_query(query_variables));
9a46751999-05-29Mirar (Pontus Hagland) }
b648852001-01-11Johan Schön array(string) post_url_nice(string|Standards.URI url,
3f3b4e2000-08-04Johan Sundström  mapping query_variables,
fdfa092000-11-26Johan Sundström  void|mapping request_headers, void|Protocols.HTTP.Query con)
f4b01a1999-04-09Mirar (Pontus Hagland) {
fdfa092000-11-26Johan Sundström  object c = post_url(url, query_variables, request_headers, con); return c && ({ c->headers["content-type"], c->data() });
f4b01a1999-04-09Mirar (Pontus Hagland) }
9a46751999-05-29Mirar (Pontus Hagland) 
b648852001-01-11Johan Schön string post_url_data(string|Standards.URI url,
3f3b4e2000-08-04Johan Sundström  mapping query_variables,
fdfa092000-11-26Johan Sundström  void|mapping request_headers, void|Protocols.HTTP.Query con)
9a46751999-05-29Mirar (Pontus Hagland) {
fdfa092000-11-26Johan Sundström  object z = post_url(url, query_variables, request_headers, con); return z && z->data();
9a46751999-05-29Mirar (Pontus Hagland) } //! Helper function for replacing HTML entities //! with the corresponding iso-8859-1 characters.
7dc3162001-04-27Henrik Grubbström (Grubba) //! @note //! All characters aren't replaced, only those with
9a46751999-05-29Mirar (Pontus Hagland) //! 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;",
edd19d2002-12-05Xavier Beaudouin  "&amp;", "&#34;", "&#39;", "&#0;", "&#58;" }),
9a46751999-05-29Mirar (Pontus Hagland)  ({ "Æ", "Á", "Â", "À", "Å", "Ã", "Ä", "Ç", "Ð", "É", "Ê", "È", "Ë", "Í", "Î", "Ì", "Ï", "Ñ", "Ó", "Ô", "Ò", "Ø", "Õ", "Ö", "Þ", "Ú", "Û", "Ù", "Ü", "Ý", "á", "â", "æ", "à", "&apos;", "å", "&ast;", "ã", "ä", "¦", "ç", "¢", ":", ",", "&commat;", "©", "°", "$", "é", "ê", "è", "&emsp;", "&ensp;", "&equals;", "ð", "ë", "!", "½", "¼", "¾", "&frac18;", "&frac38;", "&frac58;", "&frac78;", ">", ">", "&half;", "&hyphen;", "í", "î", "¡", "ì", "¿", "ï", "«", "(", "&lsqb;", "<", "<", "&mdash;", "µ", "·", "", "&ndash;", "¬", "ñ", "ó", "ô", "ò", "ø", "õ", "ö", "¶", "%", ".", "+", "±", "£", "?", "\"", "»", "®", ")", "&rsqb;", "§", "&semi;", "­", "¹", "²", "³", "ß", "þ", "~",
edd19d2002-12-05Xavier Beaudouin  "&trade;", "ú", "û", "ù", "ü", "ý", "¥", "ÿ", "&verbar;", "&", "\"", "\'", "\000", ":" }),
9a46751999-05-29Mirar (Pontus Hagland)  ); }
9fc89e2000-04-15Mirar (Pontus Hagland) //! 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:
d0d3ec2001-07-17Martin Nilsson //! @pre{
7dc3162001-04-27Henrik Grubbström (Grubba) //! > Protocols.HTTP.http_encode_query( (["anna":"eva","lilith":"blue"]) );
9fc89e2000-04-15Mirar (Pontus Hagland) //! Result: "lilith=blue&anna=eva"
7dc3162001-04-27Henrik Grubbström (Grubba) //! > Protocols.HTTP.http_encode_query( (["&amp;":"&","'=\"":"\0\0\0"]) );
9fc89e2000-04-15Mirar (Pontus Hagland) //! Result: "%26amp%3b=%26&%27%3d%22=%00%00%00"
7dc3162001-04-27Henrik Grubbström (Grubba) //! @}
9fc89e2000-04-15Mirar (Pontus Hagland) string http_encode_query(mapping(string:int|string) variables)
9a46751999-05-29Mirar (Pontus Hagland) {
9fc89e2000-04-15Mirar (Pontus Hagland)  return Array.map((array)variables,
06ba322001-11-12Anders Johansson  lambda(array(string|int|array(string)) v)
9a46751999-05-29Mirar (Pontus Hagland)  {
9fc89e2000-04-15Mirar (Pontus Hagland)  if (intp(v[1])) return http_encode_string(v[0]);
06ba322001-11-12Anders Johansson  if (arrayp(v[1])) return map(v[1], lambda (string val) { return http_encode_string(v[0])+"="+ http_encode_string(val); })*"&";
9fc89e2000-04-15Mirar (Pontus Hagland)  return http_encode_string(v[0])+"="+
06ba322001-11-12Anders Johansson  http_encode_string(v[1]);
9a46751999-05-29Mirar (Pontus Hagland)  })*"&"; }
7dc3162001-04-27Henrik Grubbström (Grubba) //! This protects all odd - see @[http_encode_query()] -
9fc89e2000-04-15Mirar (Pontus Hagland) //! characters for transfer in HTTP. //! //! Do not use this function to protect URLs, since
cbe8c92003-04-07Martin Nilsson //! it will protect URL characters like @expr{'/'@} and @expr{'?'@}.
59708e2002-12-05Xavier Beaudouin //! @param in //! The string to encode
763eec2002-12-05H. William Welliver III //! @returns
59708e2002-12-05Xavier Beaudouin //! The HTTP encoded string
7dc3162001-04-27Henrik Grubbström (Grubba) string http_encode_string(string in)
9a46751999-05-29Mirar (Pontus Hagland) { return replace(
7dc3162001-04-27Henrik Grubbström (Grubba)  in,
9a46751999-05-29Mirar (Pontus Hagland)  ({ "\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",
13bf152003-12-04Mirar (Pontus Hagland)  "\177",
9a46751999-05-29Mirar (Pontus Hagland)  "\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",
031fd82002-12-06Marcus Comstedt  " ", "%", "'", "\"", "+", "&", "=", "/",
59708e2002-12-05Xavier Beaudouin  "#", ";", "\\", "<", ">", "\t", "\n", "\r", "@" }),
9a46751999-05-29Mirar (Pontus Hagland)  ({ "%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",
13bf152003-12-04Mirar (Pontus Hagland)  "%7f",
9a46751999-05-29Mirar (Pontus Hagland)  "%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",
031fd82002-12-06Marcus Comstedt  "%20", "%25", "%27", "%22", "%2b", "%26", "%3d", "%2f",
59708e2002-12-05Xavier Beaudouin  "%23", "%3b", "%5c", "%3c", "%3e", "%09", "%0a", "%0d", "%40" }));
9a46751999-05-29Mirar (Pontus Hagland) }
d21f9d2002-12-05Xavier Beaudouin //! Encode the specified string in as to the HTTP cookie standard. //! @param f //! The string to encode. //! @returns //! The HTTP cookie encoded string.
9a46751999-05-29Mirar (Pontus Hagland) 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",
13bf152003-12-04Mirar (Pontus Hagland)  "\177",
9a46751999-05-29Mirar (Pontus Hagland)  "\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",
d21f9d2002-12-05Xavier Beaudouin  " ", "%", "'", "\"", ",", ";", "=", ":" }),
9a46751999-05-29Mirar (Pontus Hagland)  ({ "%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",
13bf152003-12-04Mirar (Pontus Hagland)  "%7f",
9a46751999-05-29Mirar (Pontus Hagland)  "%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",
d21f9d2002-12-05Xavier Beaudouin  "%20", "%25", "%27", "%22", "%2c", "%3b", "%3d", "%3a" }));
9a46751999-05-29Mirar (Pontus Hagland) }