summaryrefslogtreecommitdiff
path: root/src/libdisfluid/disfluid-cache-entry-key.h
blob: 9c2d3baf38ec59176efb08b26512dfd2614ceae0 (plain)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#ifndef DISFLUID_DISFLUID_CACHE_ENTRY_KEY_INCLUDED
# define DISFLUID_DISFLUID_CACHE_ENTRY_KEY_INCLUDED

MAYBE_UNUSED static int
compute_cache_key (const char *restrict request_scheme,
		   const char *restrict request_header,
		   const char *restrict response_header,
		   size_t max_memory, char *restrict memory);
# include <assert.h>

# include "disfluid-init.h"

static inline int
cache_entry_key_push (const char *str, size_t length, size_t *max_memory,
		      char *restrict *memory)
{
  if (length >= *max_memory)
    {
      return -2;
    }
  memcpy (*memory, str, length);
  (*memory)[length] = '\0';
  *max_memory -= length;
  *memory += length;
  return 0;
}

static inline int
cache_entry_key_find_method (const char *request_header, const char **method,
			     const char **after_method, size_t *method_length)
{
  /* The method is found at the start of the request header. It is a
     succession of ASCII upper-case letters. */
  *method = request_header;
  *method_length = 0;
  while (request_header[*method_length] >= 'A'
	 && request_header[*method_length] <= 'Z')
    {
      *method_length += 1;
    }
  *after_method = NULL;
  if (request_header[*method_length] == ' ' && *method_length > 0)
    {
      *after_method = request_header + *method_length + 1;
      return 0;
    }
  return -1;
}

static inline int
cache_entry_key_next_line (const char **txt)
{
  /* Advance *txt until after the next \r\n. */
  static const char *eol = "\r\n";
  const char *next_eol = strstr (*txt, eol);
  if (next_eol == NULL)
    {
      return -1;
    }
  *txt = next_eol + strlen (eol);
  return 0;
}

static inline int
cache_entry_key_parse_header (const char **header_line,
			      const char **header_name,
			      size_t *header_name_length,
			      const char **header_value,
			      size_t *header_value_length)
{
  static const char *separator = ":";
  static const char *line_separator = "\r\n";
  *header_name = *header_line;
  *header_name_length = 0;
  *header_value = *header_line;
  *header_value_length = 0;
  const char *colon = strstr (*header_line, separator);
  const char *next_eol = strstr (*header_line, line_separator);
  if (colon == NULL
      || next_eol == NULL
      || ((next_eol - *header_line) < (colon - *header_line)))
    {
      /* the newline happens before the colon, or there isn’t a
         newline or a colon. */
      return -1;
    }
  *header_name_length = colon - *header_line;
  *header_value = colon + strlen (separator);
  while (**header_value == ' ')
    {
      (*header_value) += 1;
    }
  *header_value_length = next_eol - *header_value;
  /* Advance to the next line. */
  *header_line = next_eol + strlen (line_separator);
  return 0;
}

static inline int
cache_entry_key_find_header (const char **header_line,
			     const char *header_name,
			     size_t header_name_length,
			     const char **header_value,
			     size_t *header_value_length)
{
  /* Return +1 if the header is not found, 0 if the header is found
     and so *header_value / *header_value_length are set, -1 if the
     header is not HTTP. */
  const char *next_header_name = NULL;
  size_t next_header_name_length;
  int error = 0;
  while (true)
    {
      if (strncmp (*header_line, "\r\n", strlen ("\r\n")) == 0)
	{
	  /* End of header. */
	  return +1;
	}
      error =
	cache_entry_key_parse_header (header_line, &next_header_name,
				      &next_header_name_length, header_value,
				      header_value_length);
      if (error != 0)
	{
	  return error;
	}
      size_t prefix_length = 0;
      if (next_header_name_length == header_name_length)
	{
	  for (prefix_length = 0; prefix_length < header_name_length;
	       prefix_length++)
	    {
	      char c = next_header_name[prefix_length];
	      char expected = header_name[prefix_length];
	      if (c >= 'A' && c <= 'Z')
		{
		  c += ('a' - 'A');
		}
	      if (expected >= 'A' && expected <= 'Z')
		{
		  expected += ('a' - 'A');
		}
	      if (c != expected)
		{
		  /* This is not the header that we are looking for. */
		  break;
		}
	    }
	  if (prefix_length == header_name_length)
	    {
	      return 0;
	    }
	}
    }
}

static inline int
cache_entry_key_find_host (const char *request_header,
			   const char **host, size_t *host_length)
{
  int error = 0;
  /* Discard the first line of the request header. */
  if (error == 0)
    {
      error = cache_entry_key_next_line (&request_header);
    }
  if (error == 0)
    {
      error =
	cache_entry_key_find_header (&request_header, "host", strlen ("host"),
				     host, host_length);
    }
  if (error == 1)
    {
      /* Host not found. Not valid in HTTP/1.1. */
      error = -1;
    }
  return error;
}

static inline int
cache_entry_key_find_all_header_values (const char *headers,
					const char *header_name,
					size_t header_name_length,
					int (*iter) (void *, const char *,
						     size_t), void *context)
{
  int error = 0;
  while (error == 0)
    {
      const char *value;
      size_t value_length;
      error =
	cache_entry_key_find_header (&headers, header_name,
				     header_name_length, &value,
				     &value_length);
      if (error == 1)
	{
	  /* Done. */
	  return 0;
	}
      if (error == 0)
	{
	  error = iter (context, value, value_length);
	}
    }
  return error;
}

struct cache_entry_key_copy_context
{
  /* Paste all values of a specified header, separated by commas. */
  size_t max_memory;
  char *memory;
  bool needs_separator;
};

static inline int
cache_entry_key_copy_cb (void *ptr, const char *value, size_t value_length)
{
  struct cache_entry_key_copy_context *context = ptr;
  int error = 0;
  if (context->needs_separator)
    {
      error =
	cache_entry_key_push (",", strlen (","), &(context->max_memory),
			      &(context->memory));
    }
  if (error == 0)
    {
      error =
	cache_entry_key_push (value, value_length, &(context->max_memory),
			      &(context->memory));
    }
  context->needs_separator = true;
  return error;
}

static inline int
cache_entry_key_paste_header_values (const char *headers,
				     const char *header_name,
				     size_t header_name_length,
				     size_t *max_memory, char **memory)
{
  struct cache_entry_key_copy_context context = {
    .max_memory = *max_memory,
    .memory = *memory,
    .needs_separator = false
  };
  int error = cache_entry_key_find_all_header_values (headers, header_name,
						      header_name_length,
						      cache_entry_key_copy_cb,
						      &context);
  *max_memory = context.max_memory;
  *memory = context.memory;
  return error;
}

struct cache_entry_key_multiple_iterator
{
  int (*iter) (void *, const char *, size_t);
  void *context;
};

static inline void
cache_entry_key_next_atom (const char **restrict line,
			   size_t *restrict line_length,
			   const char **restrict atom,
			   size_t *restrict atom_length)
{
  /* An atom starts after the optional whitespace, and ends at the end
     of the line or at the first comma, whichever comes first. */
  while (*line_length > 0 && **line == ' ')
    {
      *line_length -= 1;
      *line += 1;
    }
  *atom = *line;
  for (*atom_length = 0;
       (*atom_length < *line_length
	&& (*line)[*atom_length] != ','); (*atom_length)++)
    ;
  assert (*atom_length == *line_length || (*line)[*atom_length] == ',');
  *line += *atom_length;
  *line_length -= *atom_length;
  if (*line_length > 0)
    {
      *line += 1;		/* also consume the comma. */
      *line_length -= 1;
    }
}

static inline int
cache_entry_key_iter_multiple (void *context, const char *value,
			       size_t value_length)
{
  struct cache_entry_key_multiple_iterator *it = context;
  const char *next_atom = NULL;
  size_t next_atom_length = 0;
  int error = 0;
  while (error == 0 && value_length > 0)
    {
      cache_entry_key_next_atom (&value, &value_length, &next_atom,
				 &next_atom_length);
      error = it->iter (it->context, next_atom, next_atom_length);
    }
  return error;
}

static inline int
cache_entry_key_find_all_varying_headers (const char *response_headers,
					  int (*iter) (void *, const char *,
						       size_t), void *context)
{
  struct cache_entry_key_multiple_iterator it = {
    .iter = iter,
    .context = context
  };
  return cache_entry_key_find_all_header_values (response_headers, "vary",
						 strlen ("vary"),
						 cache_entry_key_iter_multiple,
						 &it);
}

struct cache_entry_key_vary_iterator_context
{
  /* The goal of the iterator is to construct the key. */
  const char *request_headers;
  size_t max_memory;
  char *memory;
};

static inline int
cache_entry_key_vary_iterator_cb (void *ptr, const char *next_varying_header,
				  size_t next_varying_header_length)
{
  struct cache_entry_key_vary_iterator_context *context = ptr;
  int error = 0;
  /* Push the value of each header in the request that matches
     next_varying_header, with a comma separator. After that, push
     \r\n. */
  error =
    cache_entry_key_paste_header_values (context->request_headers,
					 next_varying_header,
					 next_varying_header_length,
					 &(context->max_memory),
					 &(context->memory));
  if (error == 0)
    {
      error =
	cache_entry_key_push ("\r\n", strlen ("\r\n"), &(context->max_memory),
			      &(context->memory));
    }
  return error;
}

static int
compute_cache_key (const char *restrict request_scheme,
		   const char *restrict request_header,
		   const char *restrict response_header,
		   size_t max_memory, char *restrict memory)
{
  ensure_init ();
  /* A cache entry key is a string, of the form:
     METHOD FULL-URI\r\n
     VALUE FOR 1ST VARYING HEADER, COMBINED WITH COMMAS\r\n
     VALUE FOR 2ND VARYING HEADER, …\r\n
     \0
   */
  /* Return: 0 on success, -1 if there is an error in request or
     response header, -2 if there is not enough memory. */
  /* If the response header has multiple Vary: header, then the list
     of varying headers is the accumulated list, in response header
     order, and in-field order, with multiplicity. */
  /* If a varying header is present multiple times in the request,
     then its value in the key is the comma-separated list of all
     occurences. in the request, in the order of appearance. */
  int error = 0;
  /* First, push the method and space. */
  const char *method;
  const char *after_method;
  size_t method_length;
  if (error == 0)
    {
      error =
	cache_entry_key_find_method (request_header, &method, &after_method,
				     &method_length);
    }
  if (error == 0)
    {
      error =
	cache_entry_key_push (method, method_length, &max_memory, &memory);
    }
  if (error == 0)
    {
      error = cache_entry_key_push (" ", strlen (" "), &max_memory, &memory);
    }
  /* Push the URI scheme and :// */
  if (error == 0)
    {
      error =
	cache_entry_key_push (request_scheme, strlen (request_scheme),
			      &max_memory, &memory);
    }
  if (error == 0)
    {
      error =
	cache_entry_key_push ("://", strlen ("://"), &max_memory, &memory);
    }
  /* Find the host and push it. */
  const char *host;
  size_t host_length;
  if (error == 0)
    {
      error = cache_entry_key_find_host (after_method, &host, &host_length);
    }
  if (error == 0)
    {
      error = cache_entry_key_push (host, host_length, &max_memory, &memory);
    }
  /* Find the URI and push it. */
  if (error == 0)
    {
      const char *space_before_version = strstr (after_method, " ");
      if (space_before_version == NULL)
	{
	  error = -1;
	}
      else
	{
	  error =
	    cache_entry_key_push (after_method,
				  space_before_version - after_method,
				  &max_memory, &memory);
	}
    }
  if (error == 0)
    {
      error =
	cache_entry_key_push ("\r\n", strlen ("\r\n"), &max_memory, &memory);
    }
  /* Iterate over all varying cache headers (there might be many Vary:
     header lines in the response). */
  const char *request_headers = after_method;
  const char *response_headers = response_header;
  if (error == 0)
    {
      error = cache_entry_key_next_line (&request_headers);
    }
  if (error == 0)
    {
      error = cache_entry_key_next_line (&response_headers);
    }
  /* So request_headers is the first line of headers, and
     response_headers too. */
  if (error == 0)
    {
      struct cache_entry_key_vary_iterator_context context = {
	.request_headers = request_headers,
	.max_memory = max_memory,
	.memory = memory
      };
      error =
	cache_entry_key_find_all_varying_headers (response_headers,
						  cache_entry_key_vary_iterator_cb,
						  &context);
      max_memory = context.max_memory;
      memory = context.memory;
    }
  return error;
}

#endif /* DISFLUID_DISFLUID_CACHE_ENTRY_KEY_INCLUDED */