unit FPHTTPStatus; {$mode objfpc}{$H+} interface uses SysUtils, fphttpserver, HTTPDefs; (* construct and return the default error message for a given * HTTP defined error code *) function http_error_response(status: integer; ARequest: TFPHTTPConnectionRequest): string; implementation function error_string(status: integer; ARequest: TFPHTTPConnectionRequest): string; begin case status of 301: ; 302: ; 307: Result := '

The document has moved here.

'; 303: Result := '

The answer to your request is located ' + 'here.

'; 305: Result := '

This resource is only accessible ' + 'through the proxy' + HTTPEncode(ARequest.Location) + '
You will need to configure ' + 'your client to use that proxy.

'; 407: ; 401: Result := '

This server could not verify that you' + 'are authorized to access the document' + 'requested. Either you supplied the wrong' + 'credentials (e.g., bad password), or your' + 'browser doesn''t understand how to supply' + 'the credentials required.

'; 400: Result := '

Your browser sent a request that ' + 'this server could not understand.
' + '

'; 403: Result := '

You don''t have permission to access ' + HTTPEncode(ARequest.URI) + 'on this server.

'; 404: Result := '

The requested URL ' + HTTPEncode(ARequest.URI) + ' was not found on this server.

'; 405: Result := '

The requested method ' + HTTPEncode(ARequest.Method) + ' is not allowed for the URL ' + HTTPEncode(ARequest.URI) + '.

'; 406: Result := '

An appropriate representation of the ' + 'requested resource ' + HTTPEncode(ARequest.URI) + ' could not be found on this server.

'; 300: ; 411: Result := '

A request of the requested method ' + HTTPEncode(ARequest.Method) + ' requires a valid Content-length.
'; 412: Result := '

The precondition on the request ' + 'for the URL ' + HTTPEncode(ARequest.URI) + ' evaluated to false.

'; 501: Result := '

' + HTTPEncode(ARequest.Method) + ' to ' + HTTPEncode(ARequest.URI) + ' not supported.
' + '

'; 502: Result := '

The proxy server received an invalid ' + 'response from an upstream server.
' + '

'; 506: Result := '

A variant for the requested ' + 'resource

' +
        HTTPEncode(ARequest.URI) +
        '
is itself a negotiable resource. ' + 'This indicates a configuration error.

'; 408: Result := '

Server timeout waiting for the HTTP request from the client.

'; 410: Result := '

The requested resource
' + HTTPEncode(ARequest.URI) + '
is no longer available on this server ' + 'and there is no forwarding address.' + 'Please remove all references to this ' + 'resource.

'; 413: Result := 'The requested resource
' + HTTPEncode(ARequest.URI) + '
' + 'does not allow request data with ' + HTTPEncode(ARequest.Method) + ' requests, or the amount of data provided in' + 'the request exceeds the capacity limit.'; 414: Result := '

The requested URL''s length exceeds the capacity' + 'limit for this server.
' + '

'; 415: Result := '

The supplied request data is not in a format ' + 'acceptable for processing by this resource.

'; 416: Result := '

None of the range-specifier values in the Range ' + 'request-header field overlap the current extent ' + 'of the selected resource.

'; 417: begin if pos('Expect', ARequest.HeaderLine) <> 0 then Result := '

The expectation given in the Expect request-header' + 'field could not be met by this server.' + 'The client sent

   ' +
          HTTPEncode(ARequest.HeaderLine) + '
' else Result := '

No expectation was seen, the Expect request-header ' + 'field was not presented by the client.'; end; 422: Result := '

The server understands the media type of the' + 'request entity, but was unable to process the' + 'contained instructions.

'; 423: Result := '

The requested resource is currently locked.' + 'The lock must be released or proper identification' + 'given before the method can be applied.

'; 424: Result := '

The method could not be performed on the resource' + 'because the requested action depended on another' + 'action and that other action failed.

'; 426: Result := '

The requested resource can only be retrieved' + 'using SSL. The server is willing to upgrade the current' + 'connection to SSL, but your client doesn''t support it.' + 'Either upgrade your client, or try requesting the page' + 'using https://'; 507: Result := '

The method could not be performed on the resource' + 'because the server is unable to store the' + 'representation needed to successfully complete the' + 'request. There is insufficient free space left in' + 'your storage allocation.

'; 503: Result := '

The server is temporarily unable to service your' + 'request due to maintenance downtime or capacity' + 'problems. Please try again later.

'; 504: Result := '

The gateway did not receive a timely response' + 'from the upstream server or application.

'; 510: Result := '

A mandatory extension policy in the request is not' + 'accepted by the server for this resource.

'; else //HTTP internal server error Result := '

The server encountered an internal ' + 'error or' + 'misconfiguration and was unable to complete ' + 'your request.

' + '

Please contact the server ' + 'administrator at ' + HTTPEncode(ARequest.Connection.Server.AdminMail) + ' to inform them of the time this ' + 'error occurred,' + ' and the actions you performed just before ' + 'this error.

' + '

More information about this error ' + 'may be available' + 'in the server error log.

'; end; end; function signature(const prefix: string; ARequest: TFPHTTPConnectionRequest): string; var name: string; begin if ARequest.Connection.Server.AdminName <> '' then name := ARequest.Connection.Server.AdminName else name := ARequest.Connection.Server.AdminMail; if ARequest.Connection.Server.AdminMail <> '' then Result := prefix + '
' + ARequest.Connection.Server.ServerBanner + ' Server at ' + HTTPEncode(name) + ' Port ' + ARequest.ServerPort + '
' else Result := prefix + '
' + ARequest.Connection.Server.ServerBanner + ' Server at ' + ARequest.Connection.Server.AdminMail + ' Port ' + ARequest.ServerPort + '
'; end; function http_error_response(status: integer; ARequest: TFPHTTPConnectionRequest): string; var title: string; h1: string; begin title := Format('%d %s', [status, GetStatusCode(status)]); h1 := GetStatusCode(status); Result := '' + '' + title + '

' + h1 + '

' + error_string(status, ARequest) + signature('
', ARequest) + ''; end; end.