Reputation: 13
i've been trying to serve binary files (mp3 files as example) through cgi. The point is that i want to make files accessible only through an special script an not through the webserver itself. Till now i got:
int main() {
cgiWriteEnvironment("/CHANGE/THIS/PATH/capcgi.dat");
cgiHeaderContentType("audio/mpeg");
FILE *fp;
fp=fopen("D:/something.mp3", "r");
char buffer[4];
while (!feof(fp)) {
fread(buffer, 4, 1, fp);
printf("%x",buffer);
}
return 0;
}
It's putting out something to stdout and the browser is trying to open vlc (so the header should be correct...) but the players can't handle the data :-(
Upvotes: 1
Views: 695
Reputation: 9705
You're encoding the .mp3
file in hexidecimal, a sequence of ASCII 0s and 4s and Cs and so forth. You want fwrite()
, which will preserve the original format of the bytes you're writing. Also: a buffer size of 4 bytes will work, but I'd recommend 4k (4048) or 8k (8192) bytes instead. On a modern machine it doesn't matter, but calls to fread()
are (relatively) slow so you want to make as few as possible.
Also: this is a pretty strange thing to want. What problem are you trying to solve, and why can't a normal web server solve it?
Upvotes: 1