Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void service_shm(int segment_id) {
- char *file;
- char *response;
- int fd, len;
- struct stat sb;
- shm_thread_t *shm = &shm_thread[segment_id];
- pthread_cond_wait(&shm->cond, &shm->mutex);
- file = strdup(&shm->data[1]);
- strdecode(file, file);
- if (file[0] == '\0')
- file = "./";
- len = strlen(file);
- if (file[0] == '/' || strcmp(file, "..") == 0 || strncmp(file, "../", 3) == 0 || strstr(file, "/../") != (char *) 0 || strcmp(&(file[len - 3]), "/..") == 0) {
- free(file);
- response = make_error(400, "Bad Request", (char *) 0, "Illegal filename.");
- write_shm(segment_id, response, strlen(response));
- free(response);
- close_shm(segment_id);
- return;
- }
- if (stat(file, &sb) < 0) {
- free(file);
- response = make_error(404, "Not Found", (char *) 0, "File not found.");
- write_shm(segment_id, response, strlen(response));
- free(response);
- close_shm(segment_id);
- return;
- }
- fd = open(file, O_RDONLY, 0);
- if (fd < 0) {
- free(file);
- response = make_error(403, "Forbidden", (char *) 0, "File is protected.");
- write_shm(segment_id, response, strlen(response));
- free(response);
- close_shm(segment_id);
- return;
- }
- free(file);
- response = make_headers(200, "Ok", (char *) 0, get_mime_type(file), sb.st_size, sb.st_mtime);
- write_shm(segment_id, response, strlen(response));
- free(response);
- do {
- len = read(fd, shm->data, SHM_BUF_SIZE);
- shm->data_size = len;
- pthread_cond_signal(&shm->cond);
- pthread_cond_wait(&shm->cond, &shm->mutex);
- } while (len > 0);
- close(fd);
- close_shm(segment_id);
- }
- static void write_shm(int segment_id, char *data, int len) {
- shm_thread_t *shm = &shm_thread[segment_id];
- shm->data_size = len < SHM_BUF_SIZE ? len : SHM_BUF_SIZE;
- memcpy(shm->data, data, shm->data_size);
- pthread_cond_signal(&shm->cond);
- pthread_cond_wait(&shm->cond, &shm->mutex);
- }
- static void close_shm(int segment_id) {
- shm_thread_t *shm = &shm_thread[segment_id];
- shm->data_size = 0;
- shm->done = 1;
- pthread_cond_signal(&shm->cond);
- pthread_cond_wait(&shm->cond, &shm->mutex);
- }
Add Comment
Please, Sign In to add comment