My Project
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
fileio_msu.c
Go to the documentation of this file.
1 /*
2 START OF LICENSE STUB
3  DeDOS: Declarative Dispersion-Oriented Software
4  Copyright (C) 2017 University of Pennsylvania, Georgetown University
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 END OF LICENSE STUB
19 */
20 #include "local_msu.h"
21 #include "msu_type.h"
22 #include "msu_message.h"
23 #include "msu_calls.h"
24 #include "logging.h"
25 #include "routing_strategies.h"
26 
27 #include "webserver/httpops.h"
28 #include "webserver/write_msu.h"
29 #include "webserver/fileio_msu.h"
30 #include "webserver/cache_msu.h"
32 #include "connection-handler.h"
33 
34 static int ws_fileio_load(struct local_msu *self,
35  struct msu_msg *msg) {
36  struct response_state *resp = msg->data;
37  resp->body_len = -1;
38 
39  FILE *file = fopen(resp->path, "rb");
40  if (file == NULL) {
41  log_info("Failed to access requested file %s with errno %d", resp->path, errno);
42  resp->body_len = -1;
43  resp->header_len = generate_header(resp->header, 404, MAX_HEADER_LEN, 0, NULL);
44  } else {
45  // Get file size
46  fseek(file, 0, SEEK_END);
47  unsigned int size = ftell(file);
48  fseek(file, 0, SEEK_SET);
49  if (size > MAX_BODY_LEN) {
50  log_warn("File larger (%d) than body buffer (%d). Truncating contents.",
51  size, MAX_BODY_LEN);
52  size = MAX_BODY_LEN;
53  }
54 
55  // Allocate memory for file contents and populate it
56  resp->body_len = fread(resp->body, sizeof(char), size, file);
57  if (resp->body_len != size) {
58  log_error("Only read %d of %d bytes from file %s", resp->body_len, size, resp->path);
59  } else {
60  log_info("FileIO read %d byte from file %s", resp->body_len, resp->path);
61  }
62 
63  fclose(file);
64 
65  char *mimetype = path_to_mimetype(resp->path);
66  resp->header_len = generate_header(resp->header, 200, MAX_HEADER_LEN, resp->body_len,
67  mimetype);
68  }
69 
70  call_msu_type(self, &WEBSERVER_CACHE_MSU_TYPE, &msg->hdr, sizeof(*resp), resp);
71  call_msu_type(self, &WEBSERVER_WRITE_MSU_TYPE, &msg->hdr, sizeof(*resp), resp);
72 
73  return 0;
74 }
75 
77  .name = "Webserver_fileio_msu",
79  .receive = ws_fileio_load,
80  .route = shortest_queue_route
81 };
int shortest_queue_route(struct msu_type *type, struct local_msu *sender, struct msu_msg *msg, struct msu_endpoint *output)
Chooses the local MSU with the shortest queue.
struct msu_type WEBSERVER_FILEIO_MSU_TYPE
Definition: fileio_msu.c:76
#define log_info(fmt,...)
Definition: logging.h:88
Defines a type of MSU, including callback and accessor functions.
struct msu_type WEBSERVER_CACHE_MSU_TYPE
Definition: cache_msu.c:300
void * data
Payload.
Definition: msu_message.h:104
Logging of status messages to the terminal.
char * path_to_mimetype(char *path)
Definition: httpops.c:36
struct msu_type WEBSERVER_WRITE_MSU_TYPE
Definition: write_msu.c:69
Declares the methods available for calling an MSU from another MSU.
#define MAX_BODY_LEN
#define log_error(fmt,...)
Definition: logging.h:101
Declares the structures and functions applicable to MSUs on the local machine.
The structure that represents an MSU located on the local machine.
Definition: local_msu.h:38
static int ws_fileio_load(struct local_msu *self, struct msu_msg *msg)
Definition: fileio_msu.c:34
int call_msu_type(struct local_msu *sender, struct msu_type *dst_type, struct msu_msg_hdr *hdr, size_t data_size, void *data)
Sends an MSU message to a destination of the given type, utilizing the sending MSU's routing function...
Definition: msu_calls.c:146
Defines a type of MSU.
Definition: msu_type.h:46
struct msu_msg_hdr hdr
Definition: msu_message.h:102
int generate_header(char *dest, int code, int capacity, int body_len, char *mime_type)
Definition: httpops.c:88
A message that is to be delivered to an instance of an MSU.
Definition: msu_message.h:101
Messages passed to MSUs.
#define MAX_HEADER_LEN
#define WEBSERVER_FILEIO_MSU_TYPE_ID
Definition: fileio_msu.h:23
char * name
Name for the msu type.
Definition: msu_type.h:48
#define log_warn(fmt,...)
Definition: logging.h:113
Declares strategies that MSUs can use for routing to endpoints.