~panda-roux/MoonGem

ref: 917d3392b47714661c55f0aba66fe01f6020d118 MoonGem/src/uri.c -rw-r--r-- 4.0 KiB
917d3392 — panda-roux implementing pre-, post-, and error-script middleware functionality 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "uri.h"

#include <pcre2posix.h>

#include "log.h"
#include "util.h"

#define DEFAULT_DOCUMENT "index.gmi"
#define EXT_GMI ".gmi"
#define ILLEGAL_PATH_SEQUENCES "..", "~", "$", "*"

#define URI_PATTERN                                        \
  "(?:(?:([a-z]+):\\/\\/"                                  \
  ")?([a-z0-9]{1}[a-z0-9.\\-]+)(?::([0-9]{2,5}))?)?(?:\\/" \
  "([^\\r\\n\\?]*[^\\r\\n\\?\\/])\\/?)?(?:(?:\\/)?\\?([^\\r\\n\\?]+))?"

#define URI_PART_COUNT 6
#define URI_SCHEME 1
#define URI_HOST 2
#define URI_PORT 3
#define URI_PATH 4
#define URI_INPUT 5

#define URI_PATH_MAX 1024

static regex_t uri_regexp;
static char regex_error[512] = {0};

static bool path_is_gmi(const char* path) {
  if (path == NULL) {
    return false;
  }

  return strcmp(strrchr(path, '.'), EXT_GMI) == 0;
}

static bool path_is_illegal(const char* path) {
  const char* bad_strings[] = {ILLEGAL_PATH_SEQUENCES};
  for (int i = 0; i < sizeof(bad_strings) / sizeof(char*); ++i) {
    if (strstr(path, bad_strings[i]) != NULL) {
      return true;
    }
  }

  return false;
}

static char* extract_part(regmatch_t* match, const char* buf) {
  regoff_t start = match->rm_so;
  regoff_t end = match->rm_eo;
  return start < 0 || start == end ? NULL : strndup(&buf[start], end - start);
}

static void standardize_path(char** path) {
  // if empty path, append default document
  if (*path == NULL) {
    LOG_DEBUG("Empty path; appending default document");
    *path = strdup(DEFAULT_DOCUMENT);
    return;
  }

  size_t initial_len = strnlen(*path, URI_PATH_MAX);

  // if path ends in forward-slash or looks like a directory, grow and append
  // default document
  char* last_delim = strrchr(*path, '/');
  char* last_period = strrchr(*path, '.');
  if ((*path)[initial_len - 1] == '/' || last_period == NULL ||
      last_delim > last_period) {
    LOG_DEBUG("Path looks like a directory; appending default document");

    size_t new_len = initial_len + sizeof(DEFAULT_DOCUMENT) + 1;

    char* tmp = realloc(*path, new_len);
    if (tmp == NULL) {
      LOG_ERROR(
          "Failed to reallocate enough space to append default document onto "
          "the request path");
      free(*path);
      *path = NULL;
      return;
    }

    *path = tmp;
    strncat(&(*path)[initial_len], "/" DEFAULT_DOCUMENT,
            sizeof(DEFAULT_DOCUMENT) + 1);
  } else {
    LOG_DEBUG("Path looks like a file; leaving it as-is");
  }
}

int init_uri_regex(void) {
  int status = regcomp(&uri_regexp, URI_PATTERN, REG_ICASE);

  LOG_DEBUG("URI pattern: %s", URI_PATTERN);

  if (status != 0) {
    regerror(status, &uri_regexp, &regex_error[0], sizeof(regex_error));
    LOG_ERROR("Failed to compile URI regex: %s", &regex_error[0]);
    return -1;
  }

  return 0;
}

void cleanup_uri_regex(void) { regfree(&uri_regexp); }

uri_t* create_uri(const char* buf) {
  regmatch_t matches[URI_PART_COUNT] = {0};
  switch (regexec(&uri_regexp, buf, URI_PART_COUNT, &matches[0],
                  REG_NOTBOL | REG_NOTEOL)) {
    case REG_ESPACE:
    case REG_NOMATCH:
      return NULL;
    default:
      break;
  }

  LOG_DEBUG("Request: %s", buf);

  uri_t* uri = malloc(sizeof(uri_t));
  uri->scheme = extract_part(&matches[URI_SCHEME], buf);
  uri->host = extract_part(&matches[URI_HOST], buf);
  uri->port = extract_part(&matches[URI_PORT], buf);
  uri->path = extract_part(&matches[URI_PATH], buf);
  uri->input = extract_part(&matches[URI_INPUT], buf);

  standardize_path(&uri->path);

  // path can be null if EOM occurred trying to add default doc
  if (uri->path == NULL || path_is_illegal(uri->path)) {
    destroy_uri(uri);
    return NULL;
  }

  uri->type = path_is_gmi(uri->path) ? URI_TYPE_GEMTEXT : URI_TYPE_FILE;

  LOG_DEBUG("Scheme: %s", uri->scheme);
  LOG_DEBUG("Host:   %s", uri->host);
  LOG_DEBUG("Port:   %s", uri->port);
  LOG_DEBUG("Path:   %s", uri->path);
  LOG_DEBUG("Input:  %s", uri->input);

  return uri;
}

void destroy_uri(uri_t* uri) {
  if (uri == NULL) {
    return;
  }

  CHECK_FREE(uri->scheme);
  CHECK_FREE(uri->host);
  CHECK_FREE(uri->port);
  CHECK_FREE(uri->path);
  CHECK_FREE(uri->input);

  free(uri);
}