Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2013-02

fpweb: handling DELETE method as well as GET

5 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Reinier Olislagers

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 DocumentIDINVALIDID 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;

Reply by email

Reinier Olislagers
to Reinier Olislagers
Reinier Olislagers wrote 13 lines
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?

Oh, the Apache error log shows this for for the delete requests: Premature end of script headers: tigercgi

Reply by email

Michael Van Canneyt
to Reinier Olislagers
Reinier Olislagers wrote 16 lines
On 28-2-2013 10:58, Reinier Olislagers wrote: > 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? > Oh, the Apache error log shows this for for the delete requests: Premature end of script headers: tigercgi

I am on it. Need it myself at this very moment.

Michael.

Reply by email

Michael Van Canneyt
to Reinier Olislagers: Oh, the Apache error log shows this for for the delete requests: Premature end of script…
Reinier Olislagers wrote 16 lines
On 28-2-2013 10:58, Reinier Olislagers wrote: > 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? > Oh, the Apache error log shows this for for the delete requests: Premature end of script headers: tigercgi

Fixed in rev. 23667.

In fact, I've disabled the method checking altogether. At a later point, we can consider having a property that checks the method and enforces a HTTP spec compliant method.

Michael.

Reply by email

Reinier Olislagers
to Michael Van Canneyt
Michael Van Canneyt wrote 11 lines
On Thu, 28 Feb 2013, Reinier Olislagers wrote: > On 28-2-2013 10:58, Reinier Olislagers wrote: >> 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: Fixed in rev. 23667.

Ok, that works, thanks.

Reply by email