Интернет решения от доктора Боба | страница 17
>
> implementation
> uses
> SysUtils, Windows;
> var
> Data: String = '';
> function Value(const Field: ShortString): ShortString;
> var
> i: Integer;
> begin
> Result := '';
> i := Pos(Field+'=',Data);
> if i > 0 then
> begin
> Inc(i,Length(Field)+1);
> while Data[i] <> '&' do
> begin
> Result := Result + Data[i];
> Inc(i)
> end
> end
> end{Value};
> var
> P: PChar;
> i: Integer;
> Str: ShortString;
> type
> TRequestMethod = (Unknown,Get,Post);
> var
> RequestMethod: TRequestMethod = Unknown;
> initialization
> P := GetEnvironmentStrings;
> while P^ <> #0 do
> begin
> Str := StrPas(P);
> if Pos('REQUEST_METHOD=',Str) > 0 then
> begin
> Delete(Str,1,Pos('=',Str));
> if Str = 'POST' then RequestMethod := Post
> else
> if Str = 'GET' then RequestMethod := Get
> end;
> if Pos('CONTENT_LENGTH=',Str) = 1 then
> begin
> Delete(Str,1,Pos('=',Str));
> ContentLength := StrToInt(Str)
> end;
> if Pos('QUERY_STRING=',Str) > 0 then
> begin
> Delete(Str,1,Pos('=',Str));
> SetLength(Data,Length(Str)+1);
> Data := Str
> end;
> Inc(P, StrLen(P)+1)
> end;
> if RequestMethod = Post then
> begin
> SetLength(Data,ContentLength+1);
> for i:=1 to ContentLength do read(Data[i]);
> Data[ContentLength+1] := '&';
> { if IOResult <> 0 then { skip }
> end;
> i := 0;
> while i < Length(Data) do
> begin
> Inc(i);
> if Data[i] = '+' then Data[i] := ' ';
> if (Data[i] = '%') then{ special code }
> begin
> Str := '$00';
> Str[2] := Data[i+1];
> Str[3] := Data[i+2];
> Delete(Data,i+1,2);
> Data[i] := Chr(StrToInt(Str))
> end
> end;
> if i > 0 then Data[i+1] := '&'
> else Data := '&'
> finalization
> Data := ''
> end.
Я написал кучу CGI приложений за последний год и все они используют модуль DrBobCGI. Теперь реальное пример: стандартное CGI приложение – гостевая книга (guestbook), в которой запрашивается ваше имя и небольшой комментарий, написанное с помощью всего нескольких строк на Дельфи.
Вначале CGI форма:
>
>
>
Dr.Bob's Guestbook
>
> METHOD=POST>
> Name:
> Comments:
>
>
>
>
>
Теперь консольное приложение:
> program CGI;
> {$I-}
> {$APPTYPE CONSOLE}
> uses
> DrBobCGI;
> var