Get (HttpRequest function)

From m204wiki
Revision as of 00:10, 16 March 2016 by JAL (talk | contribs) (→‎See also: add links)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Send an HTTP GET request to the server (HttpRequest class)

The Get method sends an HTTP request to an HTTP server in the HTTP GET format: the request syntax sent to the server is the same as if a METHOD="GET" HTML form was submitted from a web browser, or as if a URL was simply clicked.

Syntax

%httpResponse = httpRequest:Get[( [[Port=] string], [[Cancel=] number], - [CertificateCheck= certificateChecker], - [CheckKeepAliveCertificate= boolean])]

Syntax terms

%httpResponse A reference to the HTTPResponse object instantiated and returned by this Get method.
httpRequest The previously defined and instantiated HttpRequest object that contains the request (its methods were used to create the request).
Port A string expression that identifies a previously defined CLSOCK port. The string default is the MASTER port, if any is defined. If you omit string and no MASTER port is defined, the request is canceled.

This is an optional and name allowed parameter. If you specify a value, you may (but don't have to) specify the parameter name, Port (case not important).

Cancel A numeric expression that controls whether the request is cancelled if a communication/socket error occurs. If zero, the request is not cancelled on these errors. If non-zero, the request is cancelled (though a message is still issued). If not specified, it defaults to 1 (cancel). See "Usage notes," below, for further discussion of error handling.

This is an optional and name allowed parameter. If you specify a value, you may (but don't have to) specify the parameter name, Cancel (case not important) if you want.

CertificateCheck This optional (name required) argument invokes an SSL certificate request from the server at the time of the method call.

The argument value (certificateChecker) must be a function, presumably local, with this method template:

Function (String):methname Is Boolean

When a connection is established, the passed function is called with the certificate (binary content, ASN.1 encoded, copied to a Longstring) as its method object.

  • If the function returns a True, the request is completed.
  • If the function returns a False, the connection is closed and HttpRequest returns a null. In this case, the request is canceled unless the Cancel argument setting prevents it.

For a CertificateCheck function example, see Examples.

CheckKeepAliveCertificate This optional, name required, argument controls how frequently a specified CertificateCheck function is invoked for a an HTTP keep-alive connection. If False, the default, the certificateChecker function is called only when the physical connection is first established and not for every HTTP request on that connection. If set to True, the CertificateCheck method is called for every request, keep-alive or not.

Usage notes

  • Two classes of non-application errors can occur when Get is invoked:
    • The HTTP request completed, but the server indicated a failure of the request (an HTTP return code of 400 or greater). An example of this is the "404 not found" message issued by a web server when a non-existent document is referenced. In this class of error, there was no TCP/IP communication error. For these errors, the request is never cancelled, and an HttpResponse object is always returned. The application handles these errors by checking the HTTPResponse Code, StatusLine, Message, and Success methods.
    • While attempting the HTTP request, a socket/communication error occurred. Examples of such errors include failure to connect to the server host, and communication failure on a TCP/IP send or receive. For this class of errors, no instance of HTTPResponse is created, and a Null object is returned. In addition, $Status is set to 1, and $StatusD is set to one of these:
      • A negative return code that indicates an error in connecting to the target HTTP server. These codes are shown below in Get method return values.
      • A positive return code indicating a communications error after the connection was made.

      The request may or may not be cancelled based on the setting of the Cancel parameter.

      Some of these errors are relatively benign (for example, inability to connect because remote partner down, timeout value exceeded), so they produce information-only messages, and they are treated as non-counting. These errors can still be detected and handled by the application (by checking for a Null result object, checking $Status and $StatusD, or both).

      Note: The distinctions between some of the return codes might or might not be meaningful, depending on the situation. For example, network problems can cause a 101 return code (indicating a timeout) or a 102 (indicating a connection closed). Connecting to a server that is using a non-HTTP protocol can result in a 100 (invalid format data received from the server) or a 101 or 102, depending on the protocol used by the server to which the request is sent: a request sent to a telnet port usually results in a timeout (return code 101), while a request sent to an SSL port without using https to indicate SSL usually results in the target server breaking the connection (return code 102).

      In general, it is risky to take action based on the distinction between the various non-zero error codes returned by this method.

    For more information about an error, you may want to issue a JANUS TRACE command with a value of 8 or 15 for the CLSOCK port that is handling your HTTP request.

  • Under Sirius Mods versions before 7.1, certain "post-oriented" actions were not allowed with an HTTP GET, and resulted in request cancellation if detected by Get function error checking:

    Under Sirius Mods 7.1 and later, these methods are allowed before a Get method call. While unusual, passing content in a Get method is not strictly forbidden by the HTTP specification.

  • For HTTP Version 1.1 mode only, the HTTP request header "connection: close" is automatically sent on GETs and POSTs to ensure that the server closes the connection with the client when the HTTP request completes. The HTTP Helper does not support persistent connections.
  • If the ports associated with the sockets in the HTTP request are not using SSL, and the CertificateCheck argument is specified, the certificateChecker function does not get called.
  • For more information about the HTTP GET request format and about the HTTP protocol, refer to http://www.w3.org/Protocols/rfc2616/rfc2616.html.

Get method return values

CodeMeaning
0 Communications was successful.
100 The format of the response from the server was invalid.
101 A Get operation exceeded its timeout value.
102 The connection was closed before the complete response was received.
-100 No free socket numbers for user.
-101 Remote host name or IP address missing or mismatch with CLSOCK port definition.
-102 Remote port number missing or mismatch with CLSOCK port definition.
-103 CLSOCK port not defined or not started.
-104 All ports on specified CLSOCK port are busy.
-105 Insufficient virtual storage.
-106 Maximum connections exceeded.
-107 Couldn't resolve remote host.
-108 Remote port not responding.
-109 Already have SOCKPMAX sockets open on this CLSOCK port.
-110 SSL/NOSSL setting mismatch.
-111 SSL handshake error.
-112 Access to CLSOCK port not enabled by ALLOW rule.
-149 Other error during connection attempt which prevents establishment of the connection.

Examples

In this code fragment, the CertificateCheck argument of Get invokes a local method that displays some information about the client socket's SSL certificate. The local method returns a True enumeration value to the Get, and the request continues:

local function (string):foo is boolean printText {~=%this:length} printText {%this:stringToHex} printText {%this:asciiToEbcdic(characterEncode=true)} return true end function ... %req is object httpRequest %resp is object httpResponse ... %resp = %req:get(certificateCheck=foo, checkKeepAliveCertificate=true) ...

This variation of the preceding example shows that the certificateChecker function (foo) may contain code to throw an exception:

class badCert Exception ... end class ... local function (string):foo is boolean throws badCert printText {~=%this:length} printText {%this:stringToHex} printText {%this:asciiToEbcdic(characterEncode=true)} return true %bad is object badCert if ... throw %bad end if end function ... %req is object httpRequest %resp is object httpResponse ... try %resp = %req:get(certificateCheck=foo, checkKeepAliveCertificate=true) catch badCert printText Caught a bad certificate stop end try ...

See also

  • You can also send an HTTP GET using the Send method.
  • For methods that work with SSL certificates, for example letting you access certificate information, see AppendCertificateInfo and its related Stringlist methods, or see X509CertificateToXmlDoc and its related String methods.