Jump to content

Trouble with combining text and binary data in the same string

- - - - -

  • Please log in to reply
6 replies to this topic

#1
cw3le

cw3le

    Newbie

  • Members
  • Pip
  • 5 posts
I am making a server that accepts request from clients via HTTP protocol. Clients request from server some image file, and it's up to server to determine if such a file exist and if it does, it sends it back to client using HTTP protocol. In other case, it sends some error message (such as "Not found" or "Bad request" etc.).

So, in order to do this, I need to create a HTTP response message through wich server sends it's reponse on cilent's request. I am making this server using Visual C++ with MFC libaries included. I'm also using CString and CFile types of data for working with strings and files.

So far I've managed to extract requested file from clients HTTP request message, to see if it exist and to send appropiate error code as a response if needed. Also, I've managed to open and read from that file in a buffer, but I am having trouble with combining that buffer, which contains binary data, with the header of the HTTP response, wich contains ordinary text.

The only solution I could think of is to write that HTTP response to some text file and to send it back to clients, although I'm not sure that's the way it's supposed to work.

Is there any way to combine that text and binary data in a single string without loosing any of data?

#2
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

A HTTP Server should send a response like as follow if requested file exists:

HTTP/1.1 200 OK
Content-Type: application/vnd.google.safebrowsing-chunk
X-Content-Type-Options: nosniff
Date: Wed, 24 Nov 2010 11:32:45 GMT
Server: Chunked Update Server
Content-Length: 114325
X-XSS-Protection: 1; mode=block
Cache-Control: public,max-age=172800
Age: 21699

s:40801:4:692
` | jp.غس& mœ1#خM iX… hbîtvشژb h€îع'ھî î h†–
تû§'„ٹ bعv|-* h„X<l¯فسغ% h|²ش- f:}Iش” j‹Wڑzt f†حوهê2Iڑ k>أt_ cB¼ؤژ¶ڑڑ,g h†>zU™Nj¨ح k
uœç®¯µ¶F iٹ[A0 hh°J•ى m¢›-p- h„eMھ'‰M¯é ^ D]U- \€Dٌـu mٹµ²ƒ5 mنپك

I'm unable to understand what problem you face: you simply add response header and append \r\n, and then simply copy file contents to rest of the buffer, and send it to client: that's it.
Can you explain a bit what problem you're facing?

My suggestion is


char *httpData= new char[SizeOfHeader + sizeofFile];

//add headers here using standard string function to httpData buffer


//read file into httpData

fwrite(httpData + SizeOfHeader + strlen("\r\n"), fileSize, 1, requestFilePointer);



Munir

#3
cw3le

cw3le

    Newbie

  • Members
  • Pip
  • 5 posts
Looking at your fwrite function, I concluded that you were using FILE as a type for files. I am using CFile.

Concerning your question, my problem is following:
- See how that HTTP response, that you posted, have text and binary part in it? Well, my problem is that I cannot combine those two in one single string (or char array) in order to create a HTTP response message. The only possible way I can think of is to send to client two separate messages - one would conatin text header and the other would contain binary data, but I don't think that's the way this reposnse system should work.

I store binary data in char array "buf". Text header is stored in char array strTemp. I created third char array httpResponse that I use to combine buf and strTemp.

#4
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

When you write something to a char pointer, char array using memcpy, it doesn't matter its a string or binary data.

Now you have binary data in 'buf' and text header in strTemp: you can create another char array as follow



int totalMsgLen = strlen(strTemp) + strlen("\r\n") + fileSize

char *MyHttpResponse = new char[totalMsgLen];  //create enough buffer memory


memset(MyHttpResponse, 0x00, totalMsgLen);

strcat(MyHttpResponse, strTemp); //write header to final buffer

strcat(MyHttpResponse, "\r\n");     //write CRLF to buffer

memcpy(MyHttpResponse + strlen(strTemp) + strlen("\r\n"), buf, fileSize); //add file contents


write(hSocket, MyHttpResponse, TotalMsgLen, 0); // write to socket  


I hope this helps!

Munir

#5
cw3le

cw3le

    Newbie

  • Members
  • Pip
  • 5 posts
I don't know man. Your solution helped me to combine text and binary data into a single char array. To see if it works, I added command to write that char array to a text file "log.txt", and as I could see, it contains everything it's supposed to have, so that part is ok. What is wrong is this: I created a sepparate function for sending that response to client and I'm passing an argument to it. That argument is of type char *, and you could guess already that it is aforementioned char array "httpResponse". The problem is that it doesn't pass complete array! It passes only text header and first few bytes of binary data; the rest is just gone!

And here's another funny part! When I'm debugging my app I add a watch to that "httpResponse" char array. Moments before it's writing to that file "log.txt", Visual Studio shows me that value of "httpResponse" contains only text header and first few bytes of binary data. But when it's written to that text file, it's written with correct value.

Know what? Thanks for your help so far. I'm so stuck into this problem, that I'm thinking to just leave it and go on. If you could help me with this somehow, I would appreciate it. If not - no hard feelings. :)

#6
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

The reason for debugger doesn't show the whole data in char array is that it tries to show you it as a string: Note that a string can only be showed upto characters before it find '\0' character. For example

char *temp = "abc\0def";

when you print the above string you ll only see temp = abc: the reason is '\0' terminates the strings for string function print, strcpy etc.

can you paste the prototype of the function or a part of the code you're having trouble with?

Munir

#7
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
You'll need to use something similar to fwrite where it allows you to specify the length of the binary data you want to write instead of stopping at the first null byte. Note that you'll also need to open the file in binary mode so that it doesn't translate "\n" (0x0a by default I think) to CR LF and screw up your binary data.
sudo rm -rf /




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users