HTTP services use a standard commuter module style routing block. This uses a case statement syntax to interpret the arguments and route the request to the appropriate destination. Here is a template:

ValidMethod = True$ ; // Assume the HTTP method is valid until proven otherwise.

Begin Case
   Case Service _EQC ''
       SelfURL = HTTP_Services('GetSelfURL')

       Begin Case
           Case HTTPMethod _EQC 'GET'      ;   GoSub Get
           Case HTTPMethod _EQC 'OPTIONS'  ;   GoSub Options
           Case Otherwise$                 ;   ValidMethod = False$
       End Case

   Case Service _EQC 'aaa'
       HTTP_Services('RunHTTPService', NextServiceHandler, NextService, NextURL)

   Case Service _EQC 'bbb'
       HTTP_Services('RunHTTPService', NextServiceHandler, NextService, NextURL)

   Case Service _EQC 'ccc'
       HTTP_Services('RunHTTPService', NextServiceHandler, NextService, NextURL)

   Case Otherwise$
       HTTP_Services('SetResponseStatus', 404, Service : ' is not a valid service request within the ' : CurrentServiceHandler : ' module.')
End Case

If Not(ValidMethod) then
   HTTP_Services('SetResponseStatus', 405, HTTPMethod : ' is not valid for this service.')
   HTTP_Services('SetResponseHeaderField', 'Allow', 'GET', True$)
   HTTP_Services('SetResponseHeaderField', 'Allow', 'OPTIONS', True$)
end

Note that the first condition checked for is an empty Service argument. As noted already, this implies that this service is the end point. What follows is a check to see what HTTP method was used. This gives the developer tight control over approved methods for a given HTTP service. If the HTTP method is invalid then a 405 status code is automatically set. If the HTTP method is valid then a call to the internal GoSub label is made to perform the required action.

If Service is populated then a check is made against a hard-coded list of options. This gives the developer another area of tight control over the API path. If an invalid segment is passed through then none of the conditions will be met and this will result in a 404 status code being set.

  • No labels