f4b01a1999-04-09Mirar (Pontus Hagland)  //! module Protocols //! submodule HTTP
f185111999-04-12Henrik Grubbström (Grubba) //! method object(Protocols.HTTP.Query) get_url(string url)
9a46751999-05-29Mirar (Pontus Hagland) //! method object(Protocols.HTTP.Query) get_url(string url,mapping query_variables)
5237cc1999-08-05Johan Sundström //! method object(Protocols.HTTP.Query) get_url(string url,mapping query_variables, mapping request_headers)
f4b01a1999-04-09Mirar (Pontus Hagland) //! 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. //!
2ba20a2000-02-24Fredrik Noring //! method object(Protocols.HTTP.Query) put_url(string url) //! method object(Protocols.HTTP.Query) put_url(string url,string file) //! method object(Protocols.HTTP.Query) put_url(string url,string file,mapping query_variables) //! method object(Protocols.HTTP.Query) put_url(string url,string file,mapping query_variables, mapping request_headers) //! 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. //! //! method object(Protocols.HTTP.Query) delete_url(string url) //! method object(Protocols.HTTP.Query) delete_url(string url,mapping query_variables) //! method object(Protocols.HTTP.Query) delete_url(string url,mapping query_variables, mapping request_headers) //! 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. //!
f185111999-04-12Henrik Grubbström (Grubba) //! method array(string) get_url_nice(string url)
9a46751999-05-29Mirar (Pontus Hagland) //! method array(string) get_url_nice(string url,mapping query_variables)
5237cc1999-08-05Johan Sundström //! method array(string) get_url_nice(string url,mapping query_variables, mapping request_headers)
30a0a51999-05-29Mirar (Pontus Hagland) //! method string get_url_data(string url) //! method string get_url_data(string url,mapping query_variables)
5237cc1999-08-05Johan Sundström //! method string get_url_data(string url,mapping query_variables, mapping request_headers)
30a0a51999-05-29Mirar (Pontus Hagland) //! Returns an array of ({content_type,data}) and just the data //! string respective,
f4b01a1999-04-09Mirar (Pontus Hagland) //! after calling the requested server for the information. //! 0 is returned upon failure.
30a0a51999-05-29Mirar (Pontus Hagland) //! //! //! method array(string) post_url_nice(string url,mapping query_variables)
5237cc1999-08-05Johan Sundström //! method array(string) post_url_nice(string url,mapping query_variables, mapping request_headers)
30a0a51999-05-29Mirar (Pontus Hagland) //! method string post_url_data(string url,mapping query_variables)
5237cc1999-08-05Johan Sundström //! method string post_url_data(string url,mapping query_variables, mapping request_headers)
30a0a51999-05-29Mirar (Pontus Hagland) //! method object(Protocols.HTTP.Query) post_url(string url,mapping query_variables)
5237cc1999-08-05Johan Sundström //! method object(Protocols.HTTP.Query) post_url(string url,mapping query_variables, mapping request_headers)
30a0a51999-05-29Mirar (Pontus Hagland) //! 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. //!
f4b01a1999-04-09Mirar (Pontus Hagland) 
5237cc1999-08-05Johan Sundström object get_url(string url,void|mapping query_variables, void|mapping request_headers)
f4b01a1999-04-09Mirar (Pontus Hagland) { object con=master()->resolv("Protocols")["HTTP"]["Query"](); string prot="http",host; int port=80; string query;
5e7e961999-08-06Johan Sundström  if(!request_headers) request_headers = ([]);
f4b01a1999-04-09Mirar (Pontus Hagland)  sscanf(url,"%[^:/]://%[^:/]:%d/%s",prot,host,port,query) == 4 || (port=80,sscanf(url,"%[^:/]://%[^:/]/%s",prot,host,query)) == 3 || (prot="http",sscanf(url,"%[^:/]:%d/%s",host,port,query)) == 3 || (port=80,sscanf(url,"%[^:/]/%s",host,query)) == 2 ||
2102dc1999-04-09Mirar (Pontus Hagland)  (host=url,query="");
f4b01a1999-04-09Mirar (Pontus Hagland)  if (prot!="http")
9a46751999-05-29Mirar (Pontus Hagland)  error("Protocols.HTTP can't handle %O or any other protocol than HTTP\n",
f4b01a1999-04-09Mirar (Pontus Hagland)  prot);
bf999d2001-05-31Martin Nilsson  if (query_variables && sizeof(query_variables))
9a46751999-05-29Mirar (Pontus Hagland)  { if (search(query,"?")!=-1) query+="&"+http_encode_query(query_variables); else query+="?"+http_encode_query(query_variables); }
f4b01a1999-04-09Mirar (Pontus Hagland)  con->sync_request(host,port,
2102dc1999-04-09Mirar (Pontus Hagland)  "GET /"+query+" HTTP/1.0",
bf999d2001-05-31Martin Nilsson  ([ "user-agent":"Mozilla/4.0 compatible (Pike HTTP client)", "host":host ]) | request_headers);
f4b01a1999-04-09Mirar (Pontus Hagland)  if (!con->ok) return 0; return con; }
2ba20a2000-02-24Fredrik Noring object put_url(string url, void|string file, void|mapping query_variables, void|mapping request_headers) { object con=master()->resolv("Protocols")["HTTP"]["Query"](); string prot="http",host; int port=80; string query; if(!request_headers) request_headers = ([]); sscanf(url,"%[^:/]://%[^:/]:%d/%s",prot,host,port,query) == 4 || (port=80,sscanf(url,"%[^:/]://%[^:/]/%s",prot,host,query)) == 3 || (prot="http",sscanf(url,"%[^:/]:%d/%s",host,port,query)) == 3 || (port=80,sscanf(url,"%[^:/]/%s",host,query)) == 2 || (host=url,query=""); if (prot!="http") error("Protocols.HTTP can't handle %O or any other protocol than HTTP\n", prot);
bf999d2001-05-31Martin Nilsson  if (query_variables && sizeof(query_variables))
2ba20a2000-02-24Fredrik Noring  { if (search(query,"?")!=-1) query+="&"+http_encode_query(query_variables); else query+="?"+http_encode_query(query_variables); } con->sync_request(host,port, "PUT /"+query+" HTTP/1.0", ([
bf999d2001-05-31Martin Nilsson  "user-agent":"Mozilla/4.0 compatible (Pike HTTP client)", "host":host
2ba20a2000-02-24Fredrik Noring  ]) | request_headers, file); if (!con->ok) return 0; return con; } object delete_url(string url, void|mapping query_variables, void|mapping request_headers) { object con=master()->resolv("Protocols")["HTTP"]["Query"](); string prot="http",host; int port=80; string query; if(!request_headers) request_headers = ([]); sscanf(url,"%[^:/]://%[^:/]:%d/%s",prot,host,port,query) == 4 || (port=80,sscanf(url,"%[^:/]://%[^:/]/%s",prot,host,query)) == 3 || (prot="http",sscanf(url,"%[^:/]:%d/%s",host,port,query)) == 3 || (port=80,sscanf(url,"%[^:/]/%s",host,query)) == 2 || (host=url,query=""); if (prot!="http") error("Protocols.HTTP can't handle %O or any other protocol than HTTP\n", prot);
bf999d2001-05-31Martin Nilsson  if (query_variables && sizeof(query_variables))
2ba20a2000-02-24Fredrik Noring  { if (search(query,"?")!=-1) query+="&"+http_encode_query(query_variables); else query+="?"+http_encode_query(query_variables); } con->sync_request(host,port, "DELETE /"+query+" HTTP/1.0",
bf999d2001-05-31Martin Nilsson  ([ "user-agent":"Mozilla/4.0 compatible (Pike HTTP client)", "host":host ]) |
2ba20a2000-02-24Fredrik Noring  request_headers); if (!con->ok) return 0; return con; }
5237cc1999-08-05Johan Sundström array(string) get_url_nice(string url,void|mapping query_variables, void|mapping request_headers)
9a46751999-05-29Mirar (Pontus Hagland) {
5237cc1999-08-05Johan Sundström  object c=get_url(url,query_variables, request_headers);
9a46751999-05-29Mirar (Pontus Hagland)  return c && ({c->headers["content-type"],c->data()}); }
5237cc1999-08-05Johan Sundström string get_url_data(string url,void|mapping query_variables, void|mapping request_headers)
9a46751999-05-29Mirar (Pontus Hagland) {
5237cc1999-08-05Johan Sundström  object z=get_url(url,query_variables, request_headers);
8683041999-05-31Mirar (Pontus Hagland)  return z && z->data();
9a46751999-05-29Mirar (Pontus Hagland) }
5237cc1999-08-05Johan Sundström object post_url(string url,mapping query_variables, void|mapping request_headers)
9a46751999-05-29Mirar (Pontus Hagland) { object con=master()->resolv("Protocols")["HTTP"]["Query"](); string prot="http",host; int port=80; string query;
5e7e961999-08-06Johan Sundström  if(!request_headers) request_headers = ([]);
9a46751999-05-29Mirar (Pontus Hagland)  sscanf(url,"%[^:/]://%[^:/]:%d/%s",prot,host,port,query) == 4 || (port=80,sscanf(url,"%[^:/]://%[^:/]/%s",prot,host,query)) == 3 || (prot="http",sscanf(url,"%[^:/]:%d/%s",host,port,query)) == 3 || (port=80,sscanf(url,"%[^:/]/%s",host,query)) == 2 || (host=url,query=""); if (prot!="http") error("Protocols.HTTP can't handle %O or any other protocol than HTTP\n", prot); con->sync_request(host,port, "POST /"+query+" HTTP/1.0",
bf999d2001-05-31Martin Nilsson  ([ "user-agent":"Mozilla/4.0 compatible (Pike HTTP client)", "host":host ]) |
5237cc1999-08-05Johan Sundström  request_headers | (["content-type":
9a46751999-05-29Mirar (Pontus Hagland)  "application/x-www-form-urlencoded"]), http_encode_query(query_variables)); if (!con->ok) return 0; return con; }
5237cc1999-08-05Johan Sundström array(string) post_url_nice(string url,mapping query_variables, void|mapping request_headers)
f4b01a1999-04-09Mirar (Pontus Hagland) {
5237cc1999-08-05Johan Sundström  object c=post_url(url,query_variables, request_headers);
f4b01a1999-04-09Mirar (Pontus Hagland)  return c && ({c->headers["content-type"],c->data()}); }
9a46751999-05-29Mirar (Pontus Hagland) 
5237cc1999-08-05Johan Sundström string post_url_data(string url,mapping query_variables, void|mapping request_headers)
9a46751999-05-29Mirar (Pontus Hagland) {
5237cc1999-08-05Johan Sundström  object z=post_url(url,query_variables, request_headers);
8683041999-05-31Mirar (Pontus Hagland)  return z && z->data();
9a46751999-05-29Mirar (Pontus Hagland) } //! //! 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;", "&", }), ); }
bf999d2001-05-31Martin Nilsson string http_encode_query(mapping(string:int|string) variables)
9a46751999-05-29Mirar (Pontus Hagland) {
bf999d2001-05-31Martin Nilsson  return Array.map((array)variables, lambda(array(string|int) v)
9a46751999-05-29Mirar (Pontus Hagland)  {
bf999d2001-05-31Martin Nilsson  if(intp(v[1])) return http_encode_string(v[0]); return http_encode_string(v[0]) + "=" + http_encode_string(v[1]);
9a46751999-05-29Mirar (Pontus Hagland)  })*"&"; } 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",
bf999d2001-05-31Martin Nilsson  " ", "%", "'", "\"", "+", "&", "=", "/", "#", ";", "\\", "<", ">" }),
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", "%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",
bf999d2001-05-31Martin Nilsson  "%20", "%25", "%27", "%22", "%2b", "%26", "%3d", "%2f", "%23", "%3b", "%5c", "%3c", "%3e" }));
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", "\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" })); }