mirror of
https://github.com/sigmasternchen/CFloor
synced 2025-03-15 04:18:55 +00:00
moved chunked transfer encoding thread definition above sendHeader function; added custom thread data to chunked transfer encoding thread
This commit is contained in:
parent
758cc85e2f
commit
e946217ec2
1 changed files with 61 additions and 55 deletions
116
src/networking.c
116
src/networking.c
|
@ -256,6 +256,67 @@ void safeEndConnection(struct connection* connection, bool force) {
|
|||
pthread_mutex_unlock(&(connection->lock));
|
||||
}
|
||||
|
||||
struct chunkedEncodingData {
|
||||
struct connection* connection;
|
||||
int readfd;
|
||||
};
|
||||
|
||||
/*
|
||||
* This thread handles chunked transfer encoding for persistent connections
|
||||
*/
|
||||
void* chunkedTransferEncodingThread(void* _data) {
|
||||
#define ENCODING_MAX_CHUNK_SIZE (512)
|
||||
|
||||
struct chunkedEncodingData* data = (struct chunkedEncodingData*) _data;
|
||||
|
||||
int dupFd = dup(data->connection->writefd);
|
||||
if (dupFd < 0) {
|
||||
error("networking: couldn't dup for chunked encoding");
|
||||
|
||||
// closing pipe should cause the handler to terminate
|
||||
close(data->readfd);
|
||||
return NULL;
|
||||
}
|
||||
FILE* writeFile = fdopen(dupFd, "w");
|
||||
if (writeFile == NULL) {
|
||||
error("networking: couldn't open FILE stream for chunked encoding");
|
||||
|
||||
// closing pipe should cause the handler to terminate
|
||||
close(data->readfd);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
debug("networking: chunked transfer encoding: using max chunk size of %lld", ENCODING_MAX_CHUNK_SIZE);
|
||||
|
||||
char c[ENCODING_MAX_CHUNK_SIZE];
|
||||
|
||||
size_t total = 0;
|
||||
size_t chunks = 0;
|
||||
int tmp;
|
||||
while((tmp = read(data->readfd, c, ENCODING_MAX_CHUNK_SIZE)) > 0) {
|
||||
fprintf(writeFile, "%x\r\n", tmp);
|
||||
fwrite(c, sizeof(char), tmp, writeFile);
|
||||
fputs("\r\n", writeFile);
|
||||
total += tmp;
|
||||
chunks++;
|
||||
}
|
||||
|
||||
debug("networking: chunked transfer encoding: %lld bytes send in %lld chunks", total, chunks);
|
||||
|
||||
if (0 == tmp) {
|
||||
// send last chunk flag
|
||||
fputs("0\r\n\r\n", writeFile);
|
||||
} else {
|
||||
error("networking: error reading from chunked input: %s", strerror(errno));
|
||||
}
|
||||
|
||||
fclose(writeFile); // close dup, fd will stay open
|
||||
|
||||
free(data);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int sendHeader(int statusCode, struct headers* headers, struct request* request) {
|
||||
debug("networking: sending headers");
|
||||
|
||||
|
@ -304,61 +365,6 @@ int sendHeader(int statusCode, struct headers* headers, struct request* request)
|
|||
return tmp;
|
||||
}
|
||||
|
||||
/*
|
||||
* This thread handles chunked transfer encoding for persistent connections
|
||||
*/
|
||||
void* chunkedTransferEncodingThread(void* data) {
|
||||
#define ENCODING_MAX_CHUNK_SIZE (512)
|
||||
|
||||
struct fileCopy* files = (struct fileCopy*) data;
|
||||
// files->closeWriteFd is not be closed
|
||||
// except on error
|
||||
|
||||
int dupFd = dup(files->writeFd);
|
||||
if (dupFd < 0) {
|
||||
error("networking: couldn't dup for chunked encoding");
|
||||
close(files->writeFd);
|
||||
}
|
||||
FILE* writeFile = fdopen(dupFd, "w");
|
||||
if (writeFile == NULL) {
|
||||
error("networking: couldn't open FILE stream for chunked encoding");
|
||||
close(dupFd);
|
||||
close(files->writeFd);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
debug("networking: chunked transfer encoding: using max chunk size of %lld", ENCODING_MAX_CHUNK_SIZE);
|
||||
|
||||
char c[ENCODING_MAX_CHUNK_SIZE];
|
||||
|
||||
size_t total = 0;
|
||||
size_t chunks = 0;
|
||||
int tmp;
|
||||
while((tmp = read(files->readFd, c, ENCODING_MAX_CHUNK_SIZE)) > 0) {
|
||||
fprintf(writeFile, "%x\r\n", tmp);
|
||||
fwrite(c, sizeof(char), tmp, writeFile);
|
||||
fputs("\r\n", writeFile);
|
||||
total += tmp;
|
||||
chunks++;
|
||||
}
|
||||
|
||||
debug("networking: chunked transfer encoding: %lld bytes send in %lld chunks", total, chunks);
|
||||
|
||||
if (0 == tmp) {
|
||||
// send last chunk flag
|
||||
fputs("0\r\n\r\n", writeFile);
|
||||
fclose(writeFile); // close dup, socket will stay open
|
||||
} else {
|
||||
error("networking: error reading from chunked input: %s", strerror(errno));
|
||||
fclose(writeFile); // close dup, fd will stay open
|
||||
close(files->writeFd); // close socket
|
||||
}
|
||||
|
||||
free(files);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* This thread calls the handler.
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue