garyM
garyM

Reputation: 892

How to get browser's socket for sendfile() in fastcgi ?? (in C)

I'm trying to file upload and download in fastcgi.

To use sendfile() I need the web server's open socket to the client (browser).

fastcgi doesn't pass it to me (I don't think).

I'm clueless on how to get the browser's socket descriptor.

I'm also open to another approach without a redirect or opening a new connection.

help is appreciated

Upvotes: 2

Views: 978

Answers (2)

Some programmer dude
Some programmer dude

Reputation: 409156

Remember that a socket and a file is mostly handled the same, so you can use the standard output file descriptor as output "socket" instead of a real socket:

#include <unistd.h>

int in_fd = open(...);

sendfile(STDOUT_FILENO, in_fd, NULL, length_of_file);

Upvotes: 1

user149341
user149341

Reputation:

You don't get a socket to the browser in FastCGI. The only socket you get directly is connected to the web server, and it's expecting FastCGI data frames, not just raw data.

The most typical solution for file downloads is the X-Sendfile header, which directs the web server to spit out a file (probably using something like sendfile() internally) instead of your response. It was introduced by Lighttpd, is supported natively by nginx, and is supported by Apache via mod_xsendfile.

Upvotes: 2

Related Questions