void stream_cleanup (struct instance *stream) { if (stream->ops && stream->ops->flush_data) { LOG_DEBUG1 ("Cleanup of stream %d required", stream->id); stream->ops->flush_data (stream); } output_clear (&stream->output); }
int output_append(output_t* output, char c) { if (output->count >= output->size) { int newsize = 1024 + 2 * output->size; output->buf = realloc(output->buf, newsize); if (output->buf == NULL) { log_err("Daemon: Out of memory\n"); output_clear(output); return -1; } output->size = newsize; } output->buf[output->count++] = c; return 0; }
int main(int argc, char **argv) { int err; parseArguments(argc, argv); if (nodaemon == 0) { err = daemonise(); if (err != 0) exit(1); err = writePidFile(pidFile); if (err != 0) exit(1); } err = signalisation(); if (err != 0) exit(1); serverSocket = openServerSocket(port); if (serverSocket == -1) exit(1); request_t req; response_t resp; output_t output; memset(&output, 0, sizeof(output_t)); while (serverSocket != -1) { memset(&req, 0, sizeof(request_t)); memset(&resp, 0, sizeof(response_t)); int client = serverSocketAccept(serverSocket); if (client == -1) continue; int r = parseRequest(client, &req, &resp); if (r != 0) { clientPrintf(client, "HTTP/1.1 %03d\r\n", resp.status); closeClient(client); continue; } if (1) { printf("path: %s\n", req.path); for (int i =0; i < req.num_args; i++) printf("arg[%d]: %s = %s\n", i, req.names[i], req.values[i]); } const char* cmdline = findCommand(req.path); if (cmdline == NULL) { log_warn("Daemon: Invalid path: '%s'\n", req.path); clientPrint(client, "HTTP/1.1 404\r\n"); closeClient(client); continue; } if (execute(&output, cmdline) != 0) { clientPrint(client, "HTTP/1.1 500\r\n"); // Internal server error closeClient(client); continue; } if ((output.count > 8) && (strncmp(output.buf, "HTTP/1.1", 8) == 0)) { clientWrite(client, output.buf, output.count); } else { clientPrintf(client, "HTTP/1.1 200\r\nContent-Length: %d\r\n\r\n", output.count); clientWrite(client, output.buf, output.count); } closeClient(client); output_clear(&output); if (req.path) free(req.path); } removePidFile(pidFile); return 0; }