我有一个winCGI可执行脚本,它返回一个图像。我正在使用Intraweb作为服务器。
我有一个用delphi制作的自定义Web服务器,我创建了一个函数来运行cgi并返回图像。
cgi来自第三方,我无法更改您的代码。查询图像时,我有从CGI返回的下一个代码:
'Content-type: image/gif'#$A'Access-Control-Allow-Origin: *'#$A#$A'GIF89a@'#1'@'#1#0#0#0'!ÿ'#$B'NETSCAPE2.0'#3#1'ÿÿ'#0'!ù'#4#0'!'#0#0#0','#0#0#0#0'@'#1'@'#1'‡'#0#0#0#1#1#1#2#2#2#3#3#3#4#4#4#5#5#5#6#6#6#7#7#7#8#8#8#9#9#9#$A#$A#$A#$B#$B#$B#$C#$C#$C#$D#$D#$D#$E#$E#$E#$F#$F#$F#$10#$10#$10#$11#$11#$11#$12#$12#$12#$13#$13#$13#$14#$14#$14#$15#$15#$15#$16#$16#$16#$17#$17#$17#$18#$18#$18#$19#........
我需要将图像发送到浏览器中的前端应用程序。
<div>
<img src="getImage(1)">
</div>
这里,getImage函数从服务器获取图像,但未显示,因为我认为我将图像从服务器返回到前端的格式有问题。如何将服务器上图像的内容文本固定为前端的有效图像?
function RunCGIOutput(CommandLine: string; Stream:TStream;Folder: string = ''): string;
const
CReadBuffer = 2400;
var
saSecurity: TSecurityAttributes;
hRead: THandle;
hWrite: THandle;
suiStartup: TStartupInfo;
piProcess: TProcessInformation;
dRead: DWORD;
Handle,WasOK:Boolean;
Buffer: array[0..CReadBuffer] of AnsiChar;
BytesRead: Cardinal;
WorkingDirP,EnvBlock:PChar;
begin
saSecurity.nLength := SizeOf(TSecurityAttributes);
saSecurity.bInheritHandle := true;
saSecurity.lpSecurityDescriptor := nil;
EnvBlock := BuildEnvBlock(True);
if Folder <> '' then WorkingDirP := PChar(Folder)
else WorkingDirP := nil;
if CreatePipe(hRead, hWrite, @saSecurity, 0) then
try
FillChar(suiStartup, SizeOf(TStartupInfo), #0);
suiStartup.cb := SizeOf(TStartupInfo);
suiStartup.hStdInput := hRead;
suiStartup.hStdOutput := hWrite;
suiStartup.hStdError := hWrite;
suiStartup.dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;
suiStartup.wShowWindow := SW_HIDE;
Handle:=CreateProcess(nil, PChar(CommandLine), @saSecurity, @saSecurity, true, CREATE_UNICODE_ENVIRONMENT, EnvBlock, WorkingDirP, suiStartup,
piProcess);
CloseHandle(hWrite);
if Handle then
try
repeat
WasOK := ReadFile(hRead, Buffer, CReadBuffer, BytesRead, nil) and (BytesRead>0);
if WasOK then
Stream.WriteBuffer(Buffer, BytesRead);
until not WasOK;
WaitForSingleObject(piProcess.hProcess, INFINITE);
finally
CloseHandle(piProcess.hThread);
CloseHandle(piProcess.hProcess);
end;
finally
CloseHandle(hRead);
StrDispose(EnvBlock);
end;
end;
//Run CGI, take image and send to browser
function TContentImaqge.Execute(aRequest: THttpRequest; aReply: THttpReply;
const aPathname: string; aSession: TIWApplication;
aParams: TStrings): boolean;
var
s,wresult,saida,LocalDoc:string;
i:integer;
Stream:TMemoryStream;
begin
Result:=True;
LocalDoc:=TIWAppInfo.GetAppPath + 'wwwroot\cgi-bin\newweb\dgate.exe';
Stream:=TMemoryStream.Create;
saida:=RunCGIOutput(LocalDoc,Stream,TIWAppInfo.GetAppPath + 'wwwroot\cgi-bin\newweb\');
Stream.Position:=0;
saida:=ReadStringFromStream(Stream, -1, IndyTextEncoding_OSDefault);
with aReply do
begin
ResetReplyType;
Code := 200;
ContentType := MIME_GIF; // MIME_HTML;
SendStream(Stream);
end;
end;
您的CGI模块生成应由IW内容处理程序按原样发送的原始HTTP response。响应消息包括:
IntraWeb的THTTPReply
似乎无法让您控制原始HTTP响应。它提供了专用的界面来分别处理状态,标题和正文。这就是为什么您需要预处理从CGI返回的流,并用空行将标头与主体分开的原因。然后,您可以通过THTTPReply
传输它们。也许您也只想将某些标头列入白名单,而忽略其余标头。
在您的代码段中,您将使用毫无意义的ReadStringFromStream
,因为无论如何您都将放弃返回的值。它将Stream
的位置移到流的末尾,但这没关系,因为aReply.SendStream(Stream)
从头开始发送整个流的内容。
另一点是,您使用IndyTextEncoding_OSDefault
作为ReadStringFromStream
的最后一个参数,这可能是错误的,因为HTTP header is encoded in ASCII因此您应该使用IndyTextEncoding_ASCII
。
尝试使用此代码:
function TContentImaqge.Execute(aRequest: THttpRequest; aReply: THttpReply;
const aPathname: string; aSession: TIWApplication;
aParams: TStrings): Boolean;
var
CGIOutput, ContentStream: TMemoryStream;
LocalDoc, Line: string;
CharPos: Integer;
begin
Result := True;
CGIOutput := TMemoryStream.Create;
try
LocalDoc := TIWAppInfo.GetAppPath + 'wwwroot\cgi-bin\newweb\dgate.exe';
RunCGIOutput(LocalDoc, CGIOutput, TIWAppInfo.GetAppPath + 'wwwroot\cgi-bin\newweb\');
if ReadLnFromStream(CGIOutput, Line, -1, IndyTextEncoding_ASCII) then
begin
{ process status line }
CharPos := Pos(' ', Line);
if CharPos > 0 then
begin
aReply.Code := StrToInt(Copy(Line, CharPos + 1, 3));
CharPos := Pos(' ', Line, CharPos);
if CharPos > 0 then
aReply.CodeText := Copy(Line, CharPos + 1);
end;
{ process headers (copy headers as they are) }
while ReadLnFromStream(CGIOutput, Line, -1, IndyTextEncoding_ASCII) and (Line <> '') do
aReply.Headers.Add(Line);
{ at this point CGIOutput.Position is at the beginning of body, so let's just copy
the content to a separate memory stream }
ContentStream := TMemoryStream.Create;
try
ContentStream.CopyFrom(CGIOutput, CGIOutput.Size - CGIOutput.Position);
except
ContentStream.Free;
raise;
end;
aReply.SendStream(ContentStream);
end
else
begin
aReply.Code := 500;
aReply.CodeText := RSHTTPInternalServerError;
aReply.WriteString('CGI module returned malformed response.');
end;
finally
CGIOutput.Free;
end;
end;
也请注意,您随附的CGI输出不包含状态行,并且直接以标题(Content-type:...)开头。如果您是从CGI获得的,则应该更新代码以处理这种情况。