void http_response_send(http_response *response, int socket_fd) { char *serialized_headers = http_response_serialize_headers(response); if (send(socket_fd, serialized_headers, strlen(serialized_headers), 0) == -1) { err("can not send headers"); } free(serialized_headers); if (response->file_path) { struct stat st; if (stat(response->file_path, &st) == 0) { int file_fd = open(response->file_path, O_RDONLY); off_t offset = 0; off_t len = st.st_size; xsendfile(socket_fd, file_fd, &offset, len); } } else if (response->raw_response) { if (send(socket_fd, response->raw_response, strlen(response->raw_response), 0) == -1) { err("can not send headers"); } } }
static size_t restore(const char *filename, int fd) { int ifd = xopen(filename, O_RDONLY); size_t size = lseek(ifd, 0, SEEK_END); lseek(ifd, 0, SEEK_SET); xsendfile(fd, ifd, NULL, size); close(ifd); return size; }