Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

TIdHTTPResponseInfo WriteContent (or not)

141 views
Skip to first unread message

WimB

unread,
Jul 10, 2008, 6:20:13 AM7/10/08
to

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? It seems that
without, the responses are also correct. Without diving into Indy10
myself, maybe anyone can shed some light on it?

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.

Remy Lebeau (TeamB)

unread,
Jul 10, 2008, 12:37:04 PM7/10/08
to

"WimB" <wim....@.....nl> wrote in message
news:4875e25d$1...@newsgroups.borland.com...

> 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


Wim Bekker

unread,
Jul 10, 2008, 5:57:10 PM7/10/08
to
Remy Lebeau (TeamB) schreef:

> 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

Remy Lebeau (TeamB)

unread,
Jul 10, 2008, 6:44:13 PM7/10/08
to

"Wim Bekker" <wim....@.....nl> wrote in message
news:4876...@newsgroups.borland.com...

> 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


0 new messages