Re: Question about CFHTTPMessageAppendBytes
Re: Question about CFHTTPMessageAppendBytes
- Subject: Re: Question about CFHTTPMessageAppendBytes
- From: Jeremy Wyld <email@hidden>
- Date: Thu, 9 Dec 2004 10:40:10 -0800
On Dec 7, 2004, at 5:58 PM, Alex Guo wrote:
According to the official description, this function is used to append
the
data specified by newBytes to the specified message object which was
created
by calling CFHTTPMessageCreateEmpty.
My question is that when I send out a request, does it make sense that
I use
this function to append HttpHeader to the request directly instead of
using
CFHTTPMessageSetHeaderFieldValue ?
For a request, I suggest using CFHTTPMessageCreateRequest and
CFHTTPMessageSetHeaderFieldValue. The behavior is guaranteed.
And, I've read the GetExample source code provided with XCode, which
gets
response by using function CFReadStreamCopyProperty as the following
shows(I
don't mean this is a miss, because this code chunk is just used to get
http
header.):
_response = (CFHTTPMessageRef)CFReadStreamCopyProperty(_stream,
kCFStreamPropertyHTTPResponseHeader);
I think, CFReadStreamCopyProperty just gets specified property, and
can't
get all response message.
It gets the request's response code and headers only. The actual bytes
or body of the response is what comes across through the stream. In
other words, you're getting the response body when you call
CFReadStreamRead.
And We should use CFHTTPMessageAppendBytes to do that like below:
void handleBytesAvailable() {
UInt8 buffer[2048];
CFIndex bytesRead = CFReadStreamRead(_stream, buffer,
sizeof(buffer));
// Less than zero is an error
if(bytesRead < 0) {
handleStreamError();
}
else if(bytesRead != 0) {
if(_response == NULL) {
_response = CFHTTPMessageCreateEmpty(kCFAllocatorDefault,
FALSE);
}
if(! CFHTTPMessageAppendBytes(_response, buffer, bytesRead)) {
printf("error: append bytes to response\n");
return;
}
}
}
Am I right? Thanks for any tip.
I assume, based upon this code and what I believe that you're asking,
is that you want to end up with one final object or structure which has
all the content for the http response. This object should include both
the headers and the body. What you have here is close, but I believe
you want to change one thing.
After successfully reading bytes, you should change
if (_response == NULL) {
_response = CFHTTPMessageCreateEmpty(kCFAllocatorDefault, FALSE);
}
You should change it to
if (_response == NULL) {
_response = (CFHTTPMessageRef)CFReadStreamCopyProperty(_stream,
kCFStreamPropertyHTTPResponseHeader);
}
By the time you successfully read bytes, there will be a response
header. Depending when you were calling CFReadStreamCopyProperty
before, it was possible that the response didn't exist. Creating an
empty one and trying to append only the bytes of the body would either
fail outright or fail in ways which were not quite so apparent.
jeremy
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden