Class WebHandler() Professional

Class function of the WebHandler class.

Description

On web connections the class HttpEndpoint instantiates objects from user defined classes that must be derived from the class WebHandler. Those sub classes implement methods being called to serve the request.

Incoming
:execute()
Handle the request and provide the content to be returned to the client.
Properties
:HttpRequest
References a HttpRequest object representing the incoming request to the web handler.
:HttpResponse
References a HttpResponse object representing the response created by the web handler.
Examples
Implement a web endpoint
// The example shows a server that implements the Endpoint 
// CurrentTime/get. Requests to other endpoints on CurrentTime 
// receive a default handling 

#include "inkey.ch" 
#include "web.ch" 

#define PORT 81 
PROCEDURE Main() 
LOCAL cPort 
LOCAL oHttpEndpoint 
LOCAL nKey 

cPort := AllTrim(Str(PORT)) 
? "Time Server sample." 
? 
? "With a web browser navigate to:" 
? 
? "     http://localhost:"+cPort+"/CurrentTime/get" 
? "     http://localhost:"+cPort+"/CurrentTime/unknown" 
? 
? "Press ESC to quit" 

// 
// The listener thread is established 
// 
oHttpEndpoint := HttpEndpoint():new( PORT, "localhost" ) 
oHttpEndpoint:start() 

// This sample does not do anything special 
nKey := 0 
DO WHILE nKey <> K_ESC 
   nKey := Inkey(1) 
ENDDO 

// 
// Stop the endpoint before the application 
// is terminated 
// 
oHttpEndpoint:stop() 

RETURN 

// 
// Handler for the current time 
// 
CLASS CurrentTime FROM WebHandler 
EXPORTED: 
  METHOD get 
  METHOD execute 
ENDCLASS 

// 
// The method :get() handles the RESTful path CurrentTime/get 
// 
METHOD CurrentTime:get() 
LOCAL cThreadId 
LOCAL cResponse := "" 

cThreadId := AllTrim(Str(ThreadId())) 

cResponse += "<!DOCTYPE html>" 
cResponse += "<html>" 
cResponse +=   "<head><title>"+ProcName()+"</title></head>" 
cResponse +=   "<body>" 
cResponse +=     "<h1>Current Time:</h1>" 
cResponse +=     "<p>"+Time()+" (HH:MM:SS)</p>" 
cResponse +=     "<small>By thread: "+cThreadId+"</small>" 
cResponse +=   "</body>" 
cResponse += "</html>" 

RETURN cResponse 

// 
// Dispatch the request to existing methods. Return a generic 
// web page for all other requests. 
// 
METHOD CurrentTime:execute( cMethodName ) 
LOCAL cResponse := "" 

IF IsMethod(SELF, cMethodName) 
  RETURN SUPER:execute( cMethodName ) 
ENDIF 

cResponse += "<!DOCTYPE html>" 
cResponse += "<html>" 
cResponse +=   "<head><title>Unhandled request</title></head>" 
cResponse +=   "<body>" 
cResponse +=     "<h1>No handler for: "+cMethodName+"</h1>" 
cResponse +=     "<p>Please try with: /CurrentTime/get</p>" 
cResponse +=     "<small>Sorry</small>" 
cResponse +=   "</body>" 
cResponse += "<html>" 

RETURN cResponse 
Feedback

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.