简体   繁体   中英

CGI and MIME type octet-stream confusion in C++ code

I am trying to develop CGI C++ code to respond to the following request:

curl --data-binary @file.bin -H "Content-Type:application/octet-stream" 
     -X POST "http://127.0.0.1/cgi-bin/server.cgi"

file.bin is a binary file of size 4100 bytes.

On the server:

getenv("CONTENT_LENGTH");

correctly gives 4100. However when I do

char* post_data = new char[len+1]; //here len = 4100
cin.read(post_data, len);

or

fgets(post_data, len, stdin);

it only reads data until it sees char value 26 . I mean if the stream doesn't include decimal values 26 my code works if it does it treats 26 as a termination character. I discovered it by wondering why it read only 214 bytes of real data.

I want to be able to send binary data to my server somehow, but am confused by this behaviour. Any clues?

It is all on Apache 2.4 on Windows 7.

A bit of false alert from my side.

If someone finds it helpful, this is my solution:

Under Windows for backward compatibility with DOS 26 (CTRl+Z) is read as EOF. The problem will be only under Windows one can force to read data from cin as binary by:

_setmode(_fileno(stdin), _O_BINARY);

and add headers:

#include <fcntl.h> 
#include <io.h> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM