Thanks in advance, WimB.
example, in serverCommandGet:
TMemoryStream* MemoryStream = new TMemoryStream();
MemoryStream->LoadFromFile(....->LocalDoc);
HttpRequest->ResponseInfo->FreeContentStream = true;
HttpRequest->ResponseInfo->ResponseNo = 200;
HttpRequest->ResponseInfo->ContentType = ContentType;
HttpRequest->ResponseInfo->ContentLength = (int)MemoryStream-
>Size;
HttpRequest->ResponseInfo->ContentStream = MemoryStream;
HttpRequest->ResponseInfo->WriteContent;
works fine, and:
TStringList* StringList = new TStringList();
StringList->LoadFromFile(HttpRequest->LocalDoc);
HttpRequest->Content = StringList->Text;
delete StringList;
AnsiString ContentText = ReplaceTags(HttpRequest);
HttpRequest->ResponseInfo->ResponseNo = 200;
HttpRequest->ResponseInfo->ContentType = ContentType;
HttpRequest->ResponseInfo->ContentText = ContentText;
HttpRequest->ResponseInfo->ContentLength = ContentText.Length();
also works fine.
> Hi, I've created a webserver that servers pages and binaries etc.
> All works well, but I'm confused about the usage of WriteContent.
> Do I need to call this, or is it automatically called?
WriteContent() is called automatically after the OnCommandGet event handler
exits, if the handler did send the response headers yet.
Gambit
> WriteContent() is called automatically after the OnCommandGet event handler
> exits, if the handler did send the response headers yet.
Thanks for your answer and it raises some other questions:
How do I know if the response headers are sent yet? I assume that if I
always call WriteContent, I'm sure that it is send.
At one point I check for the head:
if (AnsiSameText(RequestInfo->Command, "HEAD"))
HandleHead(LocalDoc,ResponseInfo);
else
HandleNormalDoc(LocalDoc,AThread,RequestInfo, ResponseInfo);
In HandleHead, I do:
ResponseInfo->ContentType = GetMIMEType(LocalDoc);
ResponseInfo->WriteHeader();
After this, OnCommandGet exists (without serving the file). Is this
correct? Does it do WriteContent?
If LocalDoc doesn't exists, I do:
ResponseInfo->ResponseNo = 404; // - Not Found
I noticed that if I leave the WriteContent out, the result is a blank
page. If I do WriteContent, I receive a small 404 Not found message. In
this case I do have to manually call WriteContent shouldn't I?
Wim
> How do I know if the response headers are sent yet?
The TIdHTTPResponseInfo::HeaderHasBeenWritten property, which is set by
WriteHeader().
Generally, you shouldn't worry about calling WriteContent() manually. Just
set the response properties and let the server send the response
automatically.
> In HandleHead, I do:
> ResponseInfo->ContentType = GetMIMEType(LocalDoc);
> ResponseInfo->WriteHeader();
>
> After this, OnCommandGet exists (without serving the file).
> Is this correct? Does it do WriteContent?
After OnCommandGet exits, WriteHeader() is called if HeaderHasBeenWritten is
false, and WriteContent() is called if there is any value assigned to the
ContentText or ContentStream property.
> I noticed that if I leave the WriteContent out, the result is a blank
> page.
Because no data is being sent to the client at all, other than the headers.
> If I do WriteContent, I receive a small 404 Not found message.
If ContentText and ContentStream() are both empty, then WriteContent() sends
a small HTML page containing just the ResponseNo and ResponseText values.
> In this case I do have to manually call WriteContent shouldn't I?
If you assign the ContentText or ContentStream, then WriteContent() is
called automatically.
Gambit