summaryrefslogtreecommitdiff
path: root/include/disfluid/cache_entry.h
blob: 7f44357e0a4a2e8b6886c525c725fef5945e4aea (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
#ifndef H_DISFLUID_CACHE_ENTRY_INCLUDED
# define H_DISFLUID_CACHE_ENTRY_INCLUDED

# include <disfluid.h>

# ifdef __cplusplus
extern "C"
{
# endif				/* __cplusplus */

  /**
   * disfluid_cache_entry: (rename-to DisfluidCacheEntry):
   *
   * One response header, body, sprinkled with metadata.
   */
  struct disfluid_cache_entry;

  /**
   * disfluid_cache_entry_size:
   * @max_key: the maximum number of bytes in the key.
   * @max_header: the maximum number of bytes in the response header.
   * @max_body: the maximum number of bytes in the response body.
   *
   * Compute the required memory size to hold an entire cache entry.
   *
   * Returns: the minimum number of bytes to allocate.
   */
  DISFLUID_API DISFLUID_CONST extern size_t
    disfluid_cache_entry_size (size_t max_key,
			       size_t max_header, size_t max_body);

  /**
   * disfluid_cache_entry_alignment:
   *
   * Compute the minimum alignment to hold a cache entry.
   *
   * Returns: the minimum alignment.
   */
  DISFLUID_API DISFLUID_CONST extern size_t
    disfluid_cache_entry_alignment (void);

  /**
   * disfluid_cache_entry_init:
   * @entry: (type DisfluidCacheEntry): an allocated memory region,
   * with a large enough size and alignment.
   * @max_key: the maximum number of bytes in the key.
   * @max_header: the maximum number of bytes in the response header.
   * @max_body: the maximum number of bytes in the response body.
   *
   * Prepare the cache entry.
   */
  DISFLUID_API extern void
    disfluid_cache_entry_init (struct disfluid_cache_entry *entry,
			       size_t max_key,
			       size_t max_header, size_t max_body);

  /**
   * disfluid_cache_entry_minimum_size:
   * @entry: (type DisfluidCacheEntry): a valid cache entry.
   * @min_key: (out): the number of bytes in the key.
   * @min_header: (out): the number of bytes in the response header.
   * @min_body: (out): the number of bytes in the response body.
   *
   * Return the number of bytes used for the key, header and body.
   */
  DISFLUID_API extern void
    disfluid_cache_entry_minimum_size (const struct disfluid_cache_entry
				       *entry, size_t *min_key,
				       size_t *min_header, size_t *min_body);

  /**
   * disfluid_cache_entry_copy:
   * @dest: (type DisfluidCacheEntry): an allocated cache entry.
   * @src: (type DisfluidCacheEntry): a different allocated cache entry.
   *
   * Try and copy @dest to @src. It can fail if @dest is too small.
   *
   * Returns: 0 on success, -1 if @dest does not have enough allocated
   * for the key, -2 for the header, -3 for the key and the header, -4
   * for the body, -5 for the key and the body, -6 for the header and
   * the body, -7 for the key, header and body.
   */
  DISFLUID_API extern int
    disfluid_cache_entry_copy (struct disfluid_cache_entry *restrict dest,
			       const struct disfluid_cache_entry *restrict
			       src);

  /**
   * disfluid_cache_entry_alloc: (constructor):
   * @max_key: the maximum number of bytes in the key.
   * @max_header: the maximum number of bytes in the response header.
   * @max_body: the maximum number of bytes in the response body.
   *
   * Allocate a new cache entry.
   *
   * Returns: (nullable) (type DisfluidCacheEntry): a new cache entry.
   */
  DISFLUID_NODISCARD DISFLUID_API
    extern struct disfluid_cache_entry
    *disfluid_cache_entry_alloc (size_t max_key,
				 size_t max_header, size_t max_body);

  /**
   * disfluid_cache_entry_dup: (constructor):
   * @entry: (type DisfluidCacheEntry): the entry to copy.
   *
   * Allocate a new cache entry as a copy of @entry, keeping the same
   * allocation limits.
   *
   * Returns: (nullable) (type DisfluidCacheEntry): a new cache entry.
   */
  DISFLUID_NODISCARD DISFLUID_API
    extern struct disfluid_cache_entry
    *disfluid_cache_entry_dup (const struct disfluid_cache_entry *entry);

  /**
   * disfluid_cache_entry_free: (skip):
   * @entry: (type DisfluidCacheEntry): the entry to destroy.
   *
   * Destroy @entry.
   */
  DISFLUID_API extern void
    disfluid_cache_entry_free (struct disfluid_cache_entry *entry);

  /**
   * disfluid_cache_entry_set_request_date: (skip):
   * @entry: (type DisfluidCacheEntry): the entry to modify.
   * @request_date: the request date.
   */
  DISFLUID_API extern void
    disfluid_cache_entry_set_request_date (struct disfluid_cache_entry *entry,
					   const struct timespec
					   *request_date);

  /**
   * disfluid_cache_entry_set_response_date: (skip):
   * @entry: (type DisfluidCacheEntry): the entry to modify.
   * @response_date: the response date.
   */
  DISFLUID_API extern void
    disfluid_cache_entry_set_response_date (struct disfluid_cache_entry
					    *entry,
					    const struct timespec
					    *response_date);

  /**
   * disfluid_cache_entry_set_invalidated:
   * @entry: (type DisfluidCacheEntry): the entry to modify.
   * @invalidated: (type boolean): whether it has been artificially invalidated.
   */
  DISFLUID_API extern void
    disfluid_cache_entry_set_invalidated (struct disfluid_cache_entry *entry,
					  int invalidated);

  /**
   * disfluid_cache_entry_invalidate:
   * @entry: (type DisfluidCacheEntry): the entry to modify.
   */
  DISFLUID_API extern void
    disfluid_cache_entry_invalidate (struct disfluid_cache_entry *entry);

  /**
   * disfluid_cache_entry_set_key:
   * @entry: (type DisfluidCacheEntry): the entry to modify.
   * @key: the new key bytes.
   *
   * Returns: 0 if the @key fits, a negative value otherwise.
   */
  DISFLUID_API extern int
    disfluid_cache_entry_set_key (struct disfluid_cache_entry *entry,
				  const char *key);

  /**
   * disfluid_cache_entry_set_response_header:
   * @entry: (type DisfluidCacheEntry): the entry to modify.
   * @header: the new response header bytes.
   *
   * Returns: 0 if the @header fits, a negative value otherwise.
   */
  DISFLUID_API extern int
    disfluid_cache_entry_set_response_header (struct disfluid_cache_entry
					      *entry, const char *header);

  /**
   * disfluid_cache_entry_set_response_body:
   * @entry: (type DisfluidCacheEntry): the entry to modify.
   * @body: (array length=body_length) (element-type char): the new
   * response body bytes.
   *
   * Returns: 0 if the @body fits, a negative value otherwise.
   */
  DISFLUID_API extern int
    disfluid_cache_entry_set_response_body (struct disfluid_cache_entry
					    *entry,
					    size_t body_length,
					    const char *body);

  /**
   * disfluid_cache_entry_get_request_date: (skip):
   * @entry: (type DisfluidCacheEntry): the entry to query.
   * @date: the request date.
   */
  DISFLUID_API extern void
    disfluid_cache_entry_get_request_date (const struct disfluid_cache_entry
					   *entry, struct timespec *date);

  /**
   * disfluid_cache_entry_get_response_date: (skip):
   * @entry: (type DisfluidCacheEntry): the entry to query.
   * @date: the response date.
   */
  DISFLUID_API extern void
    disfluid_cache_entry_get_response_date (const struct disfluid_cache_entry
					    *entry, struct timespec *date);

  /**
   * disfluid_cache_entry_is_invalidated:
   * @entry: (type DisfluidCacheEntry): the entry to query.
   *
   * Returns: (type boolean): whether @entry has been artificially invalidated.
   */
  DISFLUID_API DISFLUID_PURE extern int
    disfluid_cache_entry_is_invalidated (const struct disfluid_cache_entry
					 *entry);

  /**
   * disfluid_cache_entry_get_key:
   * @entry: (type DisfluidCacheEntry): the entry to query.
   * @start: the number of bytes of the key to skip.
   * @key: (array length=max) (element-type char): the key bytes to fill.
   *
   * Returns: the total number of key bytes.
   */
  DISFLUID_API extern size_t
    disfluid_cache_entry_get_key (const struct disfluid_cache_entry *entry,
				  size_t start, size_t max, char *key);

  /**
   * disfluid_cache_entry_get_header:
   * @entry: (type DisfluidCacheEntry): the entry to query.
   * @start: the number of bytes of the header to skip.
   * @header: (array length=max) (element-type char): the header bytes to fill.
   *
   * Returns: the total number of response header bytes.
   */
  DISFLUID_API extern size_t
    disfluid_cache_entry_get_header (const struct disfluid_cache_entry *entry,
				     size_t start, size_t max, char *header);

  /**
   * disfluid_cache_entry_get_body:
   * @entry: (type DisfluidCacheEntry): the entry to query.
   * @start: the number of bytes of the body to skip.
   * @body: (array length=max) (element-type char): the body bytes to fill.
   *
   * Returns: the total number of response body bytes.
   */
  DISFLUID_API extern size_t
    disfluid_cache_entry_get_body (const struct disfluid_cache_entry *entry,
				   size_t start, size_t max, char *body);

  /**
   * disfluid_cache_entry_load: (skip):
   * @entry: (type DisfluidCacheEntry): the entry to initialize.
   * @load_key: (type boolean): whether to actually load the key.
   * @load_header: (type boolean): whether to actually load the response header.
   * @load_body: (type boolean): whether to actually load the response body.
   * @read_impl: how to read data.
   * @skip: how to skip data.
   *
   * Returns: 0 on success, or a negative error code.
   */
  DISFLUID_NODISCARD DISFLUID_API extern int
    disfluid_cache_entry_load (struct disfluid_cache_entry *entry,
			       int load_key,
			       int load_header,
			       int load_body,
			       ssize_t (*read_impl) (void *user_data,
						     void *buffer,
						     size_t max_size),
			       int (*skip) (void *context, size_t size),
			       void *context);

  /**
   * disfluid_cache_entry_read:
   * @entry: (type DisfluidCacheEntry): the entry to initialize.
   * @load_key: (type boolean): whether to actually load the key.
   * @load_header: (type boolean): whether to actually load the response header.
   * @load_body: (type boolean): whether to actually load the response body.
   * @fd: the file descriptor to use.
   *
   * Returns: 0 on success, or a negative error code.
   */
  DISFLUID_NODISCARD DISFLUID_FD_ARG_READ_2 DISFLUID_API
    extern int disfluid_cache_entry_read (struct disfluid_cache_entry *entry,
					  int load_key,
					  int load_header,
					  int load_body, int fd);

  /**
   * disfluid_cache_entry_fread: (skip):
   * @entry: (type DisfluidCacheEntry): the entry to initialize.
   * @load_key: (type boolean): whether to actually load the key.
   * @load_header: (type boolean): whether to actually load the response header.
   * @load_body: (type boolean): whether to actually load the response body.
   * @f: the file handle to use.
   *
   * Returns: 0 on success, or a negative error code.
   */
  DISFLUID_NODISCARD DISFLUID_API extern int
    disfluid_cache_entry_fread (struct disfluid_cache_entry *entry,
				int load_key,
				int load_header, int load_body, FILE * f);

  /**
   * disfluid_cache_entry_save:
   * @entry: (type DisfluidCacheEntry): the entry to save.
   * @write_impl: how to write data.
   * @user_context: (closure write_impl): the closure data for the callback.
   *
   * Returns: 0 on success, or a negative error code.
   */
  DISFLUID_NODISCARD DISFLUID_API extern int
    disfluid_cache_entry_save (const struct disfluid_cache_entry *entry,
			       ssize_t (*write_impl) (void *context,
						      const void *buffer,
						      size_t max_size),
			       void *user_context);

  /**
   * disfluid_cache_entry_write:
   * @entry: (type DisfluidCacheEntry): the entry to save.
   * @fd: the file descriptor to use.
   *
   * Returns: 0 on success, or a negative error code.
   */
  DISFLUID_NODISCARD DISFLUID_FD_ARG_WRITE_2 DISFLUID_API
    extern int disfluid_cache_entry_write (const struct disfluid_cache_entry
					   *entry, int fd);

  /**
   * disfluid_cache_entry_fwrite:
   * @entry: (type DisfluidCacheEntry): the entry to save.
   * @f: the file handle to use.
   *
   * Returns: 0 on success, or a negative error code.
   */
  DISFLUID_NODISCARD DISFLUID_API extern int
    disfluid_cache_entry_fwrite (const struct disfluid_cache_entry *entry,
				 FILE * f);

# define DISFLUID_CACHE_ENTRY_ALLOC(ptr, max_key, max_header, max_body) \
  (ptr = disfluid_cache_entry_alloc (max_key, max_header, max_body),    \
   assert (sizeof (size_t) == sizeof (struct disfluid_cache_entry *)),  \
   size_t offset,                                                       \
   memcpy (&offset, &ptr, sizeof (size_t)),                             \
   assert (offset % disfluid_cache_entry_alignment () == 0),            \
   ptr == NULL ? -1 : 0)

# define DISFLUID_CACHE_ENTRY_FREE(ptr)    \
  { free (ptr); ptr = NULL; }

  /**
   * disfluid_cache_entry_describe_request_date:
   * @entry: (type DisfluidCacheEntry): the entry to describe.
   * @description: (out): a short phrase stating the request date.
   *
   * Returns: 0 on success, or a negative error code.
   */
  DISFLUID_NODISCARD DISFLUID_API extern int
    disfluid_cache_entry_describe_request_date (const struct
						disfluid_cache_entry *entry,
						char **description);

  /**
   * disfluid_cache_entry_describe_response_date:
   * @entry: (type DisfluidCacheEntry): the entry to describe.
   * @description: (out): a short phrase stating the response date.
   *
   * Returns: 0 on success, or a negative error code.
   */
  DISFLUID_NODISCARD DISFLUID_API extern int
    disfluid_cache_entry_describe_response_date (const struct
						 disfluid_cache_entry *entry,
						 char **description);

# ifdef __cplusplus
}
# endif				/* __cplusplus */

#endif				/* not H_DISFLUID_CACHE_ENTRY_INCLUDED */