ParseRequest
TrwRequestContent - Improved Multipart/Form-data handling in Delphi
Language: Delphi 7
Refer:
http://www.winwright.ca/index.html
This unit can be used to parse out data returned from html forms with ENCTYPE="multipart/form-data" and Method=POST Do NOT use on any other encoding.
Reason for unit is the TWebRequest class in Delphi does not provide correct handling for multipart data. These helper classes provide this handling.
Description:
Create an instance of TrwRequestContent passing to the constructor either the complete content from a TWebRequest object or the TWebRequest instance itself. In the latter case TrwRequestContent will take care of making sure all content has been retrieved from the client.
TrwRequestContent will first parse the boundary string used to delimit each form item, then use that to parse the content of all the individual fields. For each field parsed, that specific content is passed to the constructor of a TrwRequestItem which then parses it further to pull out the individual elements. For most fields this simply consists of Name and Content. For multi-select listbozes, the content will contain the choices separated by semi-colons (;). For images, the FileName and ContentType properties will also be provided. The Content will be the actual image data and can be directly saved to file as the appropriate type (e.g. .jpg or .gif).
Once created, you can iterate the list of names using the FieldName property, individual TrwRequestItems using the Field property, or get the entire TStringList containing the names and TrwRequestItem objects via the Fields property. FieldCount and Contentlength are also available.
Freeing the TrwRequestContent object will free all the TrwRequestItem objects.
{
Copyright © 2000 Winwright Inc. (Canada) All rights reserved.
This unit may be freely used and distributed by anyone for use in
any application commercial or otherwise as long as these comments
including copyright are kept intact.
This unit can be used to parse out data returned from
html forms with ENCTYPE="multipart/form-data" and Method=POST
Do NOT use on any other encoding.
Reason for unit is the TWebRequest class in Delphi does not
provide correct handling for multipart data. These helper
classes provide this handling.
Description:
Create an instance of TrwRequestContent passing to the
constructor either the complete content from a TWebRequest
object or the TWebRequest instance itself. In the latter case
TrwRequestContent will take care of making sure all content
has been retrieved from the client.
TrwRequestContent will first parse the boundary string used to
delimit each form item, then use that to parse the content of
all the individual fields. For each field parsed, that specific
content is passed to the constructor of a TrwRequestItem which
then parses it further to pull out the individual elements. For
most fields this simply consists of Name and Content. For
multi-select listbozes, the content will contain the choices
separated by semi-colons (;). For images, the FileName and
ContentType properties will also be provided. The Content will
be the actual image data and can be directly saved to file as
the appropriate type (e.g. .jpg or .gif).
Once created, you can iterate the list of names using the
FieldName property, individual TrwRequestItems using the
Field property, or get the entire TStringList containing the names and
TrwRequestItem objects via the Fields property. FieldCount and
Contentlength are also available.
Freeing the TrwRequestContent object will free all the
TrwRequestItem objects.
}
interface
uses Classes, SysUtils, HTTPApp;
type
{
TrwRequestItem:
- Contains data about a single item returned from an html form.
- There's no need to create these manually, they are created for
you by calling the constructor of the TrwRequestContent class.
}
TrwRequestItem = class
private
FName: string;
FContentType: string;
FFileName: string;
FContent: string;
FContentLength: integer;
public
constructor Create(const AContent: string);
procedure AddValue(const AContent: string);
property Name: string read FName;
property ContentType: string read FContentType;
property FileName: string read FFileName;
property Content: string read FContent;
property ContentLength: integer read FContentLength;
end;
{
TrwRequestContent:
- Passed either the Content property of a TWebRequest class,
or an instance of a TWebRequest class, will parse out the
individual fields.
}
TrwRequestContent = class
private
FList: TStrings;
FBoundary: string;
FContentLength: cardinal;
FContent: string;
procedure ClearList;
procedure ParseFields(const data: string);
function GetFieldCount: integer;
function GetField(const index: string): TrwRequestItem;
function GetFieldValue(const index: string): string;
function GetName(index: integer): string;
function GetNames: TStrings;
public
constructor Create(req: TWebRequest); overload;
constructor Create(const AData: string); overload;
destructor Destroy; override;
property ContentLength: cardinal read FContentLength;
property Content: string read FContent;
property FieldCount: integer read GetFieldCount;
property Field[const index: string]: TrwRequestItem read GetField;
property FieldValue[const index: string]: string read GetFieldValue;
property FieldName[index: integer]: string read GetName;
property FieldNames: TStrings read GetNames;
end;