My Project
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
httpops.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 "logging.h"
21 
22 #include "webserver/httpops.h"
23 
24 #define BASE_HTTP_HEADER\
25  "HTTP/1.1 200 OK\r\nContent-Type: %s\r\nContent-Length: %d\r\n\r\n"
26 
27 #define NOT_FOUND_HEADER\
28  "HTTP/1.1 404 Not Found\r\n\r\n"
29 
30 #define NOT_IMPLEMENTED_HEADER\
31  "HTTP/1.1 501 Not Implemented\r\n\r\n"
32 
33 #define DEFAULT_MIME_TYPE\
34  "text/html"
35 
36 char *path_to_mimetype(char *path) {
37  char *extension = path;
38  for (; *extension != '.' && *extension != '\0'; extension++);
39 
40  if (*extension == '\0')
41  return DEFAULT_MIME_TYPE;
42 
43  extension++; // Advance past the dot
44 
45  // TODO: Necessary to replace this by loading /etc/mime.types into hashmap?
46  if (strcasecmp(extension, "html") == 0 ||
47  strcasecmp(extension, "htm") == 0 ||
48  strcasecmp(extension, "txt") == 0) {
49  return "text/html";
50  } else if (strcasecmp(extension, "png") == 0) {
51  return "image/png";
52  } else if (strcasecmp(extension, "jpg") == 0 ||
53  strcasecmp(extension, "jpeg") == 0) {
54  return "image/jpeg";
55  } else if (strcasecmp(extension, "gif") == 0) {
56  return "image/gif";
57  }
58 
59  return DEFAULT_MIME_TYPE;
60 }
61 
62 int url_to_path(char *url, char *dir, char *path, int capacity) {
63  int len = 0;
64  for (char *c = url; *c != '\0' && *c != '?'; c++, len++);
65 
66  int dir_len = strlen(dir);
67  int dir_slash = (dir[dir_len - 1] == '/');
68  int url_slash = (url[0] == '/');
69  dir_len -= (dir_slash && url_slash);
70  int both_missing_slash = (!dir_slash && !url_slash);
71 
72  if (dir_len + len + both_missing_slash >= capacity) {
73  log_error("Path from url (%s) too large for buffer (%d)", url, capacity);
74  return -1;
75  }
76 
77  char *dest = path;
78 
79  strncpy(dest, dir, dir_len);
80  if (both_missing_slash) {
81  dest[dir_len] = '/';
82  }
83  strncpy(&dest[dir_len + both_missing_slash], url, len);
84  dest[dir_len + both_missing_slash + len] = '\0';
85  return dir_len + len;
86 }
87 
88 int generate_header(char *dest, int code, int capacity, int body_len, char *mime_type) {
89  if (code == 200) {
90  return snprintf(dest, capacity, BASE_HTTP_HEADER, mime_type, body_len);
91  } else if (code == 404) {
92  return snprintf(dest, capacity, NOT_FOUND_HEADER);
93  } else {
94  return snprintf(dest, capacity, NOT_IMPLEMENTED_HEADER);
95  }
96 }
#define BASE_HTTP_HEADER
Definition: httpops.c:24
Logging of status messages to the terminal.
char * path_to_mimetype(char *path)
Definition: httpops.c:36
#define log_error(fmt,...)
Definition: logging.h:101
#define NOT_IMPLEMENTED_HEADER
Definition: httpops.c:30
#define NOT_FOUND_HEADER
Definition: httpops.c:27
#define DEFAULT_MIME_TYPE
Definition: httpops.c:33
int generate_header(char *dest, int code, int capacity, int body_len, char *mime_type)
Definition: httpops.c:88
int url_to_path(char *url, char *dir, char *path, int capacity)
Definition: httpops.c:62