tags:

views:

47

answers:

1

This seems like a really simple task, so bear with me. I'm trying to extend a server which serves up files and webpages. Currently the server gets an HTTP request, parses it, and calls a function called sendFile:

void sendFile(ostream& ostr, std::string filename) {
    std::ifstream ifs(filename.c_str(), std::ios_base::binary);
    ostr << ifs.rdbuf();
}

This scheme currently works fine for text files like javascript and css which are in the same directory as the server binary. But when I try to serve up a png file, the browser sits forever. It seems like the difference is that the png file is not a text file, but if this is indeed the problem, what should I be using instead of ifstream? I see that the std::ios_base::binary flag is specified.

Thanks!

+1  A: 

You really should be setting the length and the mime type in the http headers.

Daniel A. White