[fpc-pascal] fpweb: handling DELETE method as well as GET

Reinier Olislagers reinierolislagers at gmail.com
Thu Feb 28 10:58:22 CET 2013


Using fpweb, I'm trying to handle DELETE requests.

The code below works for GET calls:
GET /cgi-bin/tigercgi/document/ HTTP/1.1" 200
but gives a 500 server error for DELETE calls:
DELETE /cgi-bin/tigercgi/document/ HTTP/1.1" 500

The code doesn't even seem to hit the function:
AResponse.Contents.Add('<p>Got request method: '+ARequest.Method+'</p>');
doesn't seem to be hit.

How should I fix this?

Thanks,
Reinier

procedure TFPWebdocument.DataModuleRequest(Sender: TObject; ARequest:
TRequest;
  AResponse: TResponse; var Handled: Boolean);
// We don't define any actions but handle the request at the module
level before any actions would be evaluated.
{
DELETE http://server/cgi-bin/tigercgi/document/    //delete all docs?!?!
GET    http://server/cgi-bin/tigercgi/document/    //list of docs
DELETE http://server/cgi-bin/tigercgi/document/304 //remove document
with id 304
GET    http://server/cgi-bin/tigercgi/document/304 //get document with
id 304
}
var
  DocumentID: integer;
  IsValidRequest: boolean;
  StrippedPath: string;
begin
  IsValidRequest:=false;
  {
  pathinfo apparently returns something like
  /document/304
  StrippedPath will remove trailing and leading /
  }
  StrippedPath:=copy(ARequest.PathInfo,2,Length(ARequest.PathInfo));
  if RightStr(StrippedPath,1)='/' then
StrippedPath:=Copy(StrippedPath,1,Length(StrippedPath)-1);
  AResponse.Contents.Add('<p>todo: debug; document module</p>');
  AResponse.Contents.Add('<p>Got request method: '+ARequest.Method+'</p>');
  // Make sure the user didn't specify levels in the URI we don't support:
  case ARequest.Method of
    'DELETE':
    begin
      case WordCount(StrippedPath,['/']) of
        1: //http://server/cgi-bin/tigercgi/document/
        begin
          IsValidRequest:=true;
          //todo: delete every document
          AResponse.Contents.Add('<p>todo delete all documents</p>');
        end;
        2: //http://server/cgi-bin/tigercgi/document/304
        begin
          DocumentID:=StrToIntDef(ExtractWord(2,StrippedPath,['/']),
INVALIDID);
          if DocumentID<>INVALIDID then IsValidRequest:=true;
          //todo: delete given document
          AResponse.Contents.Add('<p>todo delete document
'+inttostr(documentid)+'</p>');
        end;
      end;
    end;
    'GET':
    begin
      case WordCount(StrippedPath,['/']) of
        1: //http://server/cgi-bin/tigercgi/document/
        begin
          IsValidRequest:=true;
          //todo: get every document
          AResponse.Contents.Add('<p>todo get all documents</p>');
        end;
....
  if not(IsValidRequest) then
  begin
    AResponse.Code:=404;
    AResponse.CodeText:='Document not found.';
    AResponse.Contents.Add('<p>Document not found/invalid request</p>');
  end;
  Handled:=true;



More information about the fpc-pascal mailing list