  
  [1X7. A client side implementation of the HTTP protocol[0X
  
  The  [5XIO[0X  package  contains  an implementation of the client side of the HTTP
  protocol. The basic purpose of this is of course to be able to download data
  from  web  servers  from  the  [5XGAP[0X  language. However, the HTTP protocol can
  perform a much bigger variety of tasks.
  
  
  [1X7.1 Functions for client side HTTP[0X
  
  [1X7.1-1 OpenHTTPConnection[0X
  
  [2X> OpenHTTPConnection( [0X[3Xhostname, port[0X[2X ) _____________________________[0Xfunction
  [6XReturns:[0X  a record
  
  The  first argument [3Xhostname[0X must be a string containing the hostname of the
  server  to connect. The second argument [3Xport[0X must be an integer in the range
  from 1 to 65535 and describes the port to connect to on the server.
  
  The  function  opens  a TCP/IP connection to the server and returns a record
  [9Xconn[0X with the following components: [9Xconn.sock[0X is [9Xfail[0X if an error occurs and
  otherwise a [10XFile[0X object linked to the file descriptor of the socket. In case
  of  an  error,  the component [9Xconn.errormsg[0X contains an error message, it is
  otherwise empty. If everything went well then the component [9Xconn.host[0X is the
  result  from  the  host  name lookup (see [2XIO_gethostbyname[0X ([14X3.2-21[0X)) and the
  component [9Xconn.closed[0X is set to [9Xfalse[0X.
  
  No data is sent or received on the socket in this function.
  
  [1X7.1-2 HTTPRequest[0X
  
  [2X> HTTPRequest( [0X[3Xconn, method, uri, header, body, target[0X[2X ) ___________[0Xfunction
  [6XReturns:[0X  a record
  
  This function performs a complete HTTP request. The first argument must be a
  connection  record  as  returned  by a successful call to [2XOpenHTTPConnection[0X
  ([14X7.1-1[0X).  The  argument [3Xmethod[0X must be a valid HTTP request "method" in form
  of a string. The most common will be [9XGET[0X, [9XPOST[0X, or [9XHEAD[0X. The argument [3Xuri[0X is
  a string containing the URI of the request, which is given in the first line
  of  the request. This will usually be a relative or absolute path name given
  to the server. The argument [3Xheader[0X must be a [5XGAP[0X record. Each bound field of
  this  record  will  we transformed into one header line with the name of the
  component  being  the  key and the value the value. All bound values must be
  strings.  The  argument  [3Xbody[0X  must  either be a string or [9Xfalse[0X. If it is a
  string, this string is sent away as the body of the request. If no string or
  an  empty  string  is  given,  no  body  will  be  sent.  The  header  field
  [9XContent-Length[0X  is automatically created from the length of the string [3Xbody[0X.
  Finally,  the argument [3Xtarget[0X can either be [9Xfalse[0X or a string. In the latter
  case,  the  body  of the request answer is written to the file with the name
  given  in  [3Xtarget[0X. The [9Xbody[0X component of the result will be the file name in
  this  case.  If  [3Xtarget[0X is false, the full body of the answer is stored into
  the [9Xbody[0X component of the result.
  
  The  function sends away the request and awaits the answer. If anything goes
  wrong  during  the  transfer  (for  example  if  the  connection  is  broken
  prematurely), then the component [9Xstatuscode[0X of the resulting record is 0 and
  the  component  [9Xstatus[0X  is  a corresponding error message. In that case, all
  other  fields  may or may not be bound to sensible values, according to when
  the  error occurred. If everything goes well, then [9Xstatuscode[0X and [9Xstatus[0X are
  bound to the corresponding values coming from the request answer. [9Xstatuscode[0X
  is  transformed  into  a  [5XGAP[0X  integer.  The header of the answer is parsed,
  transformed  into  a [5XGAP[0X record, and stored into the component [9Xheader[0X of the
  result.  The  [9Xbody[0X component of the result record is set as described above.
  Finally,  the  [9Xprotoversion[0X  component  contains  the  HTTP protocol version
  number  used  by  the  server  as  a  string  and  the  boolean value [9Xclosed[0X
  indicates, whether or not the function has detected, that the connection has
  been  closed  by  the server. Note that by default, the connection will stay
  open, at least for a certain time after the end of the request.
  
  See  the description of the global variable [2XHTTPTimeoutForSelect[0X ([14X7.1-3[0X) for
  rules how timeouts are done in this function.
  
  Note that if the [3Xmethod[0X is [9XHEAD[0X, then no body is expected (none will be sent
  anyway) and the function returns immediately with empty body. Of course, the
  [9XContent-Length[0X  value  in  the  header is as if it the request would be done
  with the [9XGET[0X method.
  
  [1X7.1-3 HTTPTimeoutForSelect[0X
  
  [2X> HTTPTimeoutForSelect_______________________________________[0Xglobal variable
  
  This  global  variable  holds a list of length two. By default, both entries
  are  [9Xfail[0X  indicating that [2XHTTPRequest[0X ([14X7.1-2[0X) should never timeout and wait
  forever  for  an answer. Actually, the two values in this variable are given
  to the [2XIO_Select[0X ([14X4.3-3[0X) function call during I/O multiplexing. That is, the
  first  number  is  in  seconds and the second in milliseconds. Together they
  lead  to  a  timeout  for  the  HTTP  request. If a timeout occurs, an error
  condition  is triggered which returns a record with status code 0 and status
  being the timeout error message.
  
  You  can  change  the  timeout  by  accessing  the two entries of this write
  protected list variable directly.
  
  [1X7.1-4 CloseHTTPConnection[0X
  
  [2X> CloseHTTPConnection( [0X[3Xconn[0X[2X ) ______________________________________[0Xfunction
  [6XReturns:[0X  nothing
  
  Closes  the connection described by the connection record [3Xconn[0X. No error can
  possibly occur.
  
  [1X7.1-5 SingleHTTPRequest[0X
  
  [2X> SingleHTTPRequest( [0X[3Xhostname, port, method, uri, header, body, target[0X[2X ) [0Xfunction
  [6XReturns:[0X  a record
  
  The   arguments   are   as   the   corresponding   ones   in  the  functions
  [2XOpenHTTPConnection[0X   ([14X7.1-1[0X)  and  [2XHTTPRequest[0X  ([14X7.1-2[0X)  respectively.  This
  function  opens  an  HTTP  connection,  tries  a  single  HTTP  request  and
  immediately   closes  the  connection  again.  The  result  is  as  for  the
  [2XHTTPRequest[0X  ([14X7.1-2[0X)  function. If an error occurs during the opening of the
  connection, the [9Xstatuscode[0X value of the result is 0 and the error message is
  stored in the [9Xstatus[0X component of the result.
  
  The  previous function allows for a very simple implementation of a function
  that checks, whether your current [5XGAP[0X installation is up to date:
  
  [1X7.1-6 CheckForUpdates[0X
  
  [2X> CheckForUpdates( [0X[3X[0X[2X ) ______________________________________________[0Xfunction
  [6XReturns:[0X  nothing
  
  This function accesses a web page in St. Andrews and runs some [5XGAP[0X code from
  there.  This  code  knows all the currently released versions of [5XGAP[0X and its
  packages.  It prints out a summary and possibly suggests upgrades. If you do
  not  want  to  executed  code downloaded from the internet, then do not call
  this function.
  
  More         concretely,         the         page         accessed        is
  [7Xhttp://www.gap-system.org/Download/upgrade.html[0X  and  the code executed is a
  single call to the function [10XSuggestUpgrades[0X function in the [5XGAP[0X library.
  
