repo_name string | dataset string | owner string | lang string | func_name string | code string | docstring string | url string | sha string |
|---|---|---|---|---|---|---|---|---|
zap | github_2023 | zigzap | c | mustache_on_section_test | static int32_t mustache_on_section_test(mustache_section_s *section,
const char *name, uint32_t name_len,
uint8_t callable) {
FIOBJ o = fiobj_mustache_find_obj(section, name, name_len);
if (!o || FIOBJ_TYPE_IS(o, FIOBJ_T_FALSE))
return 0;
if (FIOBJ_TYPE_IS(o, FIOBJ_T_ARRAY))
return fiobj_ary_count(o);
return 1;
(void)callable; /* FIOBJ doesn't support lambdas */
} | /**
* Called for nested sections, must return the number of objects in the new
* subsection (depending on the argument's name).
*
* Arrays should return the number of objects in the array.
*
* `true` values should return 1.
*
* `false` values should return 0.
*
* A return value of -1 will stop processing with an error.
*
* Please note, this will handle both normal and inverted sections.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_mustache.c#L191-L201 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | mustache_on_section_start | static int mustache_on_section_start(mustache_section_s *section,
char const *name, uint32_t name_len,
uint32_t index) {
FIOBJ o = fiobj_mustache_find_obj(section, name, name_len);
if (!o)
return -1;
if (FIOBJ_TYPE_IS(o, FIOBJ_T_ARRAY))
section->udata2 = (void *)fiobj_ary_index(o, index);
else
section->udata2 = (void *)o;
return 0;
} | /**
* Called when entering a nested section.
*
* `index` is a zero based index indicating the number of repetitions that
* occurred so far (same as the array index for arrays).
*
* A return value of -1 will stop processing with an error.
*
* Note: this is a good time to update the subsection's `udata` with the value
* of the array index. The `udata` will always contain the value or the parent's
* `udata`.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_mustache.c#L215-L226 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | mustache_on_formatting_error | static void mustache_on_formatting_error(void *udata1, void *udata2) {
(void)udata1;
(void)udata2;
} | /**
* Called for cleanup in case of error.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_mustache.c#L231-L234 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fiobj_num_new_bignum | FIOBJ fiobj_num_new_bignum(intptr_t num) {
fiobj_num_s *o = fio_malloc(sizeof(*o));
if (!o) {
perror("ERROR: fiobj number couldn't allocate memory");
exit(errno);
}
*o = (fiobj_num_s){
.head =
{
.type = FIOBJ_T_NUMBER,
.ref = 1,
},
.i = num,
};
return (FIOBJ)o;
} | /* *****************************************************************************
Number API
***************************************************************************** */
/** Creates a Number object. Remember to use `fiobj_free`. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_numbers.c#L106-L121 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fiobj_num_tmp | FIOBJ fiobj_num_tmp(intptr_t num) {
static __thread fiobj_num_s ret;
ret = (fiobj_num_s){
.head = {.type = FIOBJ_T_NUMBER, .ref = ((~(uint32_t)0) >> 4)},
.i = num,
};
return (FIOBJ)&ret;
} | /** Mutates a Big Number object's value. Effects every object's reference! */
// void fiobj_num_set(FIOBJ target, intptr_t num) {
// assert(FIOBJ_TYPE_IS(target, FIOBJ_T_NUMBER) &&
// FIOBJ_IS_ALLOCATED(target)); obj2num(target)->i = num;
// }
/** Creates a temporary Number object. This ignores `fiobj_free`. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_numbers.c#L130-L137 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fiobj_float_new | FIOBJ fiobj_float_new(double num) {
fiobj_float_s *o = fio_malloc(sizeof(*o));
if (!o) {
perror("ERROR: fiobj float couldn't allocate memory");
exit(errno);
}
*o = (fiobj_float_s){
.head =
{
.type = FIOBJ_T_FLOAT,
.ref = 1,
},
.f = num,
};
return (FIOBJ)o;
} | /* *****************************************************************************
Float API
***************************************************************************** */
/** Creates a Float object. Remember to use `fiobj_free`. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_numbers.c#L144-L159 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fiobj_float_set | void fiobj_float_set(FIOBJ obj, double num) {
assert(FIOBJ_TYPE_IS(obj, FIOBJ_T_FLOAT));
obj2float(obj)->f = num;
} | /** Mutates a Float object's value. Effects every object's reference! */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_numbers.c#L162-L165 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fiobj_float_tmp | FIOBJ fiobj_float_tmp(double num) {
static __thread fiobj_float_s ret;
ret = (fiobj_float_s){
.head =
{
.type = FIOBJ_T_FLOAT,
.ref = ((~(uint32_t)0) >> 4),
},
.f = num,
};
return (FIOBJ)&ret;
} | /** Creates a temporary Number object. This ignores `fiobj_free`. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_numbers.c#L168-L179 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fio_str2str | static fio_str_info_s fio_str2str(const FIOBJ o) {
return fiobj_str_get_cstr(o);
} | /* *****************************************************************************
String VTables
***************************************************************************** */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_str.c#L60-L62 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fiobj_str_buf | FIOBJ fiobj_str_buf(size_t capa) {
if (capa)
capa = capa + 1;
else
capa = PAGE_SIZE;
fiobj_str_s *s = fio_malloc(sizeof(*s));
if (!s) {
perror("ERROR: fiobj string couldn't allocate memory");
exit(errno);
}
*s = (fiobj_str_s){
.head =
{
.ref = 1,
.type = FIOBJ_T_STRING,
},
.str = FIO_STR_INIT,
};
if (capa) {
fio_str_capa_assert(&s->str, capa);
}
return ((uintptr_t)s | FIOBJECT_STRING_FLAG);
} | /* *****************************************************************************
String API
***************************************************************************** */
/** Creates a buffer String object. Remember to use `fiobj_free`. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_str.c#L106-L129 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fiobj_str_new | FIOBJ fiobj_str_new(const char *str, size_t len) {
fiobj_str_s *s = fio_malloc(sizeof(*s));
if (!s) {
perror("ERROR: fiobj string couldn't allocate memory");
exit(errno);
}
*s = (fiobj_str_s){
.head =
{
.ref = 1,
.type = FIOBJ_T_STRING,
},
.str = FIO_STR_INIT,
};
if (str && len) {
fio_str_write(&s->str, str, len);
}
return ((uintptr_t)s | FIOBJECT_STRING_FLAG);
} | /** Creates a String object. Remember to use `fiobj_free`. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_str.c#L132-L150 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fiobj_str_move | FIOBJ fiobj_str_move(char *str, size_t len, size_t capacity) {
fiobj_str_s *s = fio_malloc(sizeof(*s));
if (!s) {
perror("ERROR: fiobj string couldn't allocate memory");
exit(errno);
}
*s = (fiobj_str_s){
.head =
{
.ref = 1,
.type = FIOBJ_T_STRING,
},
.str = FIO_STR_INIT_EXISTING(str, len, capacity),
};
return ((uintptr_t)s | FIOBJECT_STRING_FLAG);
} | /**
* Creates a String object. Remember to use `fiobj_free`.
*
* It's possible to wrap a previosly allocated memory block in a FIOBJ String
* object, as long as it was allocated using `fio_malloc`.
*
* The ownership of the memory indicated by `str` will "move" to the object and
* will be freed (using `fio_free`) once the object's reference count drops to
* zero.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_str.c#L162-L177 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fiobj_str_tmp | FIOBJ fiobj_str_tmp(void) {
static __thread fiobj_str_s tmp = {
.head =
{
.ref = ((~(uint32_t)0) >> 4),
.type = FIOBJ_T_STRING,
},
.str = {.small = 1},
};
tmp.str.frozen = 0;
fio_str_resize(&tmp.str, 0);
return ((uintptr_t)&tmp | FIOBJECT_STRING_FLAG);
} | /**
* Returns a thread-static temporary string. Avoid calling `fiobj_dup` or
* `fiobj_free`.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_str.c#L183-L195 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fiobj_str_freeze | void fiobj_str_freeze(FIOBJ str) {
if (FIOBJ_TYPE_IS(str, FIOBJ_T_STRING))
fio_str_freeze(&obj2str(str)->str);
} | /** Prevents the String object from being changed. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_str.c#L198-L201 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fiobj_str_capa_assert | size_t fiobj_str_capa_assert(FIOBJ str, size_t size) {
assert(FIOBJ_TYPE_IS(str, FIOBJ_T_STRING));
if (obj2str(str)->str.frozen)
return 0;
fio_str_info_s state = fio_str_capa_assert(&obj2str(str)->str, size);
return state.capa;
} | /** Confirms the requested capacity is available and allocates as required. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_str.c#L204-L211 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fiobj_str_capa | size_t fiobj_str_capa(FIOBJ str) {
assert(FIOBJ_TYPE_IS(str, FIOBJ_T_STRING));
return fio_str_capa(&obj2str(str)->str);
} | /** Return's a String's capacity, if any. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_str.c#L214-L217 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fiobj_str_resize | void fiobj_str_resize(FIOBJ str, size_t size) {
assert(FIOBJ_TYPE_IS(str, FIOBJ_T_STRING));
fio_str_resize(&obj2str(str)->str, size);
obj2str(str)->hash = 0;
return;
} | /** Resizes a String object, allocating more memory if required. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_str.c#L220-L225 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fiobj_str_compact | void fiobj_str_compact(FIOBJ str) {
assert(FIOBJ_TYPE_IS(str, FIOBJ_T_STRING));
fio_str_compact(&obj2str(str)->str);
return;
} | /** Deallocates any unnecessary memory (if supported by OS). */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_str.c#L228-L232 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fiobj_str_clear | void fiobj_str_clear(FIOBJ str) {
assert(FIOBJ_TYPE_IS(str, FIOBJ_T_STRING));
fio_str_resize(&obj2str(str)->str, 0);
obj2str(str)->hash = 0;
} | /** Empties a String's data. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_str.c#L235-L239 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fiobj_str_write | size_t fiobj_str_write(FIOBJ dest, const char *data, size_t len) {
assert(FIOBJ_TYPE_IS(dest, FIOBJ_T_STRING));
if (obj2str(dest)->str.frozen)
return 0;
obj2str(dest)->hash = 0;
return fio_str_write(&obj2str(dest)->str, data, len).len;
} | /**
* Writes data at the end of the string, resizing the string as required.
* Returns the new length of the String
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_str.c#L245-L251 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fiobj_str_write_i | size_t fiobj_str_write_i(FIOBJ dest, int64_t num) {
assert(FIOBJ_TYPE_IS(dest, FIOBJ_T_STRING));
if (obj2str(dest)->str.frozen)
return 0;
obj2str(dest)->hash = 0;
return fio_str_write_i(&obj2str(dest)->str, num).len;
} | /**
* Writes a number at the end of the String using normal base 10 notation.
*
* Returns the new length of the String
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_str.c#L258-L264 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fiobj_str_printf | size_t fiobj_str_printf(FIOBJ dest, const char *format, ...) {
assert(FIOBJ_TYPE_IS(dest, FIOBJ_T_STRING));
if (obj2str(dest)->str.frozen)
return 0;
obj2str(dest)->hash = 0;
va_list argv;
va_start(argv, format);
fio_str_info_s state = fio_str_vprintf(&obj2str(dest)->str, format, argv);
va_end(argv);
return state.len;
} | /**
* Writes data at the end of the string, resizing the string as required.
* Returns the new length of the String
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_str.c#L270-L280 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fiobj_str_readfile | size_t fiobj_str_readfile(FIOBJ dest, const char *filename, intptr_t start_at,
intptr_t limit) {
fio_str_info_s state =
fio_str_readfile(&obj2str(dest)->str, filename, start_at, limit);
return state.len;
} | /** Dumps the `filename` file's contents at the end of a String. If `limit ==
* 0`, than the data will be read until EOF.
*
* If the file can't be located, opened or read, or if `start_at` is beyond
* the EOF position, NULL is returned.
*
* Remember to use `fiobj_free`.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_str.c#L299-L304 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fiobj_str_concat | size_t fiobj_str_concat(FIOBJ dest, FIOBJ obj) {
assert(FIOBJ_TYPE_IS(dest, FIOBJ_T_STRING));
if (obj2str(dest)->str.frozen)
return 0;
obj2str(dest)->hash = 0;
fio_str_info_s o = fiobj_obj2cstr(obj);
if (o.len == 0)
return fio_str_len(&obj2str(dest)->str);
return fio_str_write(&obj2str(dest)->str, o.data, o.len).len;
} | /**
* Writes data at the end of the string, resizing the string as required.
* Returns the new length of the String
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_str.c#L310-L319 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fiobj_str_hash | uint64_t fiobj_str_hash(FIOBJ o) {
assert(FIOBJ_TYPE_IS(o, FIOBJ_T_STRING));
// if (obj2str(o)->is_small) {
// return fiobj_hash_string(STR_INTENAL_STR(o), STR_INTENAL_LEN(o));
// } else
if (obj2str(o)->hash) {
return obj2str(o)->hash;
}
fio_str_info_s state = fio_str_info(&obj2str(o)->str);
obj2str(o)->hash = fiobj_hash_string(state.data, state.len);
return obj2str(o)->hash;
} | /**
* Calculates a String's SipHash value for use as a HashMap key.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_str.c#L324-L335 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fio_atol | int64_t __attribute__((weak)) fio_atol(char **pstr) {
return strtoll(*pstr, pstr, 0);
} | /**
* We include this in case the parser is used outside of facil.io.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobject.c#L73-L75 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fio_atof | double __attribute__((weak)) fio_atof(char **pstr) {
return strtod(*pstr, pstr);
} | /**
* We include this in case the parser is used outside of facil.io.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobject.c#L81-L83 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fiobj_each2 | size_t fiobj_each2(FIOBJ o, int (*task)(FIOBJ obj, void *arg), void *arg) {
if (!o || !FIOBJ_IS_ALLOCATED(o) || (FIOBJECT2VTBL(o)->each == NULL)) {
task(o, arg);
return 1;
}
/* run task for root object */
if (task(o, arg) == -1)
return 1;
uintptr_t pos = 0;
fiobj_stack_s stack = FIO_ARY_INIT;
struct task_packet_s packet = {
.task = task,
.arg = arg,
.stack = &stack,
.counter = 1,
};
do {
if (!pos)
packet.next = 0;
packet.incomplete = 0;
pos = FIOBJECT2VTBL(o)->each(o, pos, fiobj_task_wrapper, &packet);
if (packet.stop)
goto finish;
if (packet.incomplete) {
fiobj_stack_push(&stack, pos);
fiobj_stack_push(&stack, o);
}
if (packet.next) {
fiobj_stack_push(&stack, (FIOBJ)0);
fiobj_stack_push(&stack, packet.next);
}
o = FIOBJ_INVALID;
fiobj_stack_pop(&stack, &o);
fiobj_stack_pop(&stack, &pos);
} while (o);
finish:
fiobj_stack_free(&stack);
return packet.counter;
} | /**
* Single layer iteration using a callback for each nested fio object.
*
* Accepts any `FIOBJ ` type but only collections (Arrays and Hashes) are
* processed. The container itself (the Array or the Hash) is **not** processed
* (unlike `fiobj_each2`).
*
* The callback task function must accept an object and an opaque user pointer.
*
* Hash objects pass along a `FIOBJ_T_COUPLET` object, containing
* references for both the key and the object. Keys shouldn't be altered once
* placed as a key (or the Hash will break). Collections (Arrays / Hashes) can't
* be used as keeys.
*
* If the callback returns -1, the loop is broken. Any other value is ignored.
*
* Returns the "stop" position, i.e., the number of items processed + the
* starting point.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobject.c#L341-L380 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fiobj_dealloc_task | static void fiobj_dealloc_task(FIOBJ o, void *stack_) {
// if (!o)
// fprintf(stderr, "* WARN: freeing a NULL no-object\n");
// else
// fprintf(stderr, "* freeing object %s\n", fiobj_obj2cstr(o).data);
if (!o || !FIOBJ_IS_ALLOCATED(o))
return;
if (OBJREF_REM(o))
return;
if (!FIOBJECT2VTBL(o)->each || !FIOBJECT2VTBL(o)->count(o)) {
FIOBJECT2VTBL(o)->dealloc(o, NULL, NULL);
return;
}
fiobj_stack_s *s = stack_;
fiobj_stack_push(s, o);
} | /* *****************************************************************************
Free complex objects (objects with nesting)
***************************************************************************** */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobject.c#L386-L401 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fiobj_free_complex_object | void fiobj_free_complex_object(FIOBJ o) {
fiobj_stack_s stack = FIO_ARY_INIT;
do {
FIOBJECT2VTBL(o)->dealloc(o, fiobj_dealloc_task, &stack);
} while (!fiobj_stack_pop(&stack, &o));
fiobj_stack_free(&stack);
} | /**
* Decreases an object's reference count, releasing memory and
* resources.
*
* This function affects nested objects, meaning that when an Array or
* a Hash object is passed along, it's children (nested objects) are
* also freed.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobject.c#L410-L416 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fiobj_iseq____internal_complex__ | int fiobj_iseq____internal_complex__(FIOBJ o, FIOBJ o2) {
// if (FIOBJECT2VTBL(o)->each && FIOBJECT2VTBL(o)->count(o))
// return int fiobj_iseq____internal_complex__(const FIOBJ o, const FIOBJ
// o2);
fiobj_stack_s left = FIO_ARY_INIT, right = FIO_ARY_INIT, queue = FIO_ARY_INIT;
do {
fiobj_each1(o, 0, fiobj_iseq____internal_complex__task, &left);
fiobj_each1(o2, 0, fiobj_iseq____internal_complex__task, &right);
while (fiobj_stack_count(&left)) {
o = FIOBJ_INVALID;
o2 = FIOBJ_INVALID;
fiobj_stack_pop(&left, &o);
fiobj_stack_pop(&right, &o2);
if (!fiobj_iseq_simple(o, o2))
goto unequal;
if (FIOBJ_IS_ALLOCATED(o) && FIOBJECT2VTBL(o)->each &&
FIOBJECT2VTBL(o)->count(o)) {
fiobj_stack_push(&queue, o);
fiobj_stack_push(&queue, o2);
}
}
o = FIOBJ_INVALID;
o2 = FIOBJ_INVALID;
fiobj_stack_pop(&queue, &o2);
fiobj_stack_pop(&queue, &o);
if (!fiobj_iseq_simple(o, o2))
goto unequal;
} while (o);
fiobj_stack_free(&left);
fiobj_stack_free(&right);
fiobj_stack_free(&queue);
return 1;
unequal:
fiobj_stack_free(&left);
fiobj_stack_free(&right);
fiobj_stack_free(&queue);
return 0;
} | /** used internally for complext nested tests (Array / Hash types) */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobject.c#L446-L483 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fiobject___noop_dealloc | void fiobject___noop_dealloc(FIOBJ o, void (*task)(FIOBJ, void *), void *arg) {
(void)o;
(void)task;
(void)arg;
} | /* *****************************************************************************
Defaults / NOOPs
***************************************************************************** */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobject.c#L489-L493 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fio_tls_alpn_add | void __attribute__((weak))
fio_tls_alpn_add(void *tls, const char *protocol_name,
void (*callback)(intptr_t uuid, void *udata_connection,
void *udata_tls),
void *udata_tls, void (*on_cleanup)(void *udata_tls)) {
FIO_LOG_FATAL("HTTP SSL/TLS required but unavailable!");
exit(-1);
(void)tls;
(void)protocol_name;
(void)callback;
(void)on_cleanup;
(void)udata_tls;
} | /* *****************************************************************************
SSL/TLS patch
***************************************************************************** */
/**
* Adds an ALPN protocol callback to the SSL/TLS context.
*
* The first protocol added will act as the default protocol to be selected.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L45-L57 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_set_header | int http_set_header(http_s *r, FIOBJ name, FIOBJ value) {
if (HTTP_INVALID_HANDLE(r) || !name) {
fiobj_free(value);
return -1;
}
set_header_add(r->private_data.out_headers, name, value);
return 0;
} | /**
* Sets a response header, taking ownership of the value object, but NOT the
* name object (so name objects could be reused in future responses).
*
* Returns -1 on error and 0 on success.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L163-L170 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_set_header2 | int http_set_header2(http_s *r, fio_str_info_s n, fio_str_info_s v) {
if (HTTP_INVALID_HANDLE(r) || !n.data || !n.len || (v.data && !v.len))
return -1;
FIOBJ tmp = fiobj_str_new(n.data, n.len);
int ret = http_set_header(r, tmp, fiobj_str_new(v.data, v.len));
fiobj_free(tmp);
return ret;
} | /**
* Sets a response header.
*
* Returns -1 on error and 0 on success.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L176-L183 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_send_body | int http_send_body(http_s *r, void *data, uintptr_t length) {
if (HTTP_INVALID_HANDLE(r))
return -1;
if (!length || !data) {
http_finish(r);
return 0;
}
add_content_length(r, length);
// add_content_type(r);
add_date(r);
return ((http_vtable_s *)r->private_data.vtbl)
->http_send_body(r, data, length);
} | /**
* Sends the response headers and body.
*
* Returns -1 on error and 0 on success.
*
* AFTER THIS FUNCTION IS CALLED, THE `http_s` OBJECT IS NO LONGER VALID.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L336-L348 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_sendfile | int http_sendfile(http_s *r, int fd, uintptr_t length, uintptr_t offset) {
if (HTTP_INVALID_HANDLE(r)) {
close(fd);
return -1;
};
add_content_length(r, length);
add_content_type(r);
add_date(r);
return ((http_vtable_s *)r->private_data.vtbl)
->http_sendfile(r, fd, length, offset);
} | /**
* Sends the response headers and the specified file (the response's body).
*
* Returns -1 on error and 0 on success.
*
* AFTER THIS FUNCTION IS CALLED, THE `http_s` OBJECT IS NO LONGER VALID.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L356-L366 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_sendfile2 | int http_sendfile2(http_s *h, const char *prefix, size_t prefix_len,
const char *encoded, size_t encoded_len) {
if (HTTP_INVALID_HANDLE(h))
return -1;
struct stat file_data = {.st_size = 0};
static uint64_t accept_enc_hash = 0;
if (!accept_enc_hash)
accept_enc_hash = fiobj_hash_string("accept-encoding", 15);
static uint64_t range_hash = 0;
if (!range_hash)
range_hash = fiobj_hash_string("range", 5);
/* create filename string */
FIOBJ filename = fiobj_str_tmp();
if (prefix && prefix_len) {
/* start with prefix path */
if (encoded && prefix[prefix_len - 1] == '/' && encoded[0] == '/')
--prefix_len;
fiobj_str_capa_assert(filename, prefix_len + encoded_len + 4);
fiobj_str_write(filename, prefix, prefix_len);
}
{
/* decode filename in cases where it's URL encoded */
fio_str_info_s tmp = fiobj_obj2cstr(filename);
if (encoded) {
char *pos = (char *)encoded;
const char *end = encoded + encoded_len;
while (pos < end) {
if (*pos == '%') {
// decode hex value (this is a percent encoded value).
if (hex2byte((uint8_t *)tmp.data + tmp.len, (uint8_t *)pos + 1))
return -1;
tmp.len++;
pos += 3;
} else
tmp.data[tmp.len++] = *(pos++);
}
tmp.data[tmp.len] = 0;
fiobj_str_resize(filename, tmp.len);
/* test for path manipulations after decoding */
if (http_test_encoded_path(tmp.data + prefix_len, tmp.len - prefix_len))
return -1;
}
if (tmp.data[tmp.len - 1] == '/')
fiobj_str_write(filename, "index.html", 10);
}
/* test for file existance */
int file = -1;
uint8_t is_gz = 0;
fio_str_info_s s = fiobj_obj2cstr(filename);
{
FIOBJ tmp = fiobj_hash_get2(h->headers, accept_enc_hash);
if (!tmp)
goto no_gzip_support;
fio_str_info_s ac_str = fiobj_obj2cstr(tmp);
if (!ac_str.data || !strstr(ac_str.data, "gzip"))
goto no_gzip_support;
if (s.data[s.len - 3] != '.' || s.data[s.len - 2] != 'g' ||
s.data[s.len - 1] != 'z') {
fiobj_str_write(filename, ".gz", 3);
s = fiobj_obj2cstr(filename);
if (!stat(s.data, &file_data) &&
(S_ISREG(file_data.st_mode) || S_ISLNK(file_data.st_mode))) {
is_gz = 1;
goto found_file;
}
fiobj_str_resize(filename, s.len - 3);
}
}
no_gzip_support:
if (stat(s.data, &file_data) ||
!(S_ISREG(file_data.st_mode) || S_ISLNK(file_data.st_mode)))
return -1;
found_file:
/* set last-modified */
{
FIOBJ tmp = fiobj_str_buf(32);
fiobj_str_resize(
tmp, http_time2str(fiobj_obj2cstr(tmp).data, file_data.st_mtime));
http_set_header(h, HTTP_HEADER_LAST_MODIFIED, tmp);
}
/* set cache-control */
http_set_header(h, HTTP_HEADER_CACHE_CONTROL, fiobj_dup(HTTP_HVALUE_MAX_AGE));
/* set & test etag */
uint64_t etag = (uint64_t)file_data.st_size;
etag ^= (uint64_t)file_data.st_mtime;
etag = fiobj_hash_string(&etag, sizeof(uint64_t));
FIOBJ etag_str = fiobj_str_buf(32);
fiobj_str_resize(etag_str,
fio_base64_encode(fiobj_obj2cstr(etag_str).data,
(void *)&etag, sizeof(uint64_t)));
/* set */
http_set_header(h, HTTP_HEADER_ETAG, etag_str);
/* test */
{
static uint64_t none_match_hash = 0;
if (!none_match_hash)
none_match_hash = fiobj_hash_string("if-none-match", 13);
FIOBJ tmp2 = fiobj_hash_get2(h->headers, none_match_hash);
if (tmp2 && fiobj_iseq(tmp2, etag_str)) {
h->status = 304;
http_finish(h);
return 0;
}
}
/* handle range requests */
int64_t offset = 0;
int64_t length = file_data.st_size;
{
static uint64_t ifrange_hash = 0;
if (!ifrange_hash)
ifrange_hash = fiobj_hash_string("if-range", 8);
FIOBJ tmp = fiobj_hash_get2(h->headers, ifrange_hash);
if (tmp && fiobj_iseq(tmp, etag_str)) {
fiobj_hash_delete2(h->headers, range_hash);
} else {
tmp = fiobj_hash_get2(h->headers, range_hash);
if (tmp) {
/* range ahead... */
if (FIOBJ_TYPE_IS(tmp, FIOBJ_T_ARRAY))
tmp = fiobj_ary_index(tmp, 0);
fio_str_info_s range = fiobj_obj2cstr(tmp);
if (!range.data || memcmp("bytes=", range.data, 6))
goto open_file;
char *pos = range.data + 6;
int64_t start_at = 0, end_at = 0;
start_at = fio_atol(&pos);
if (start_at >= file_data.st_size)
goto open_file;
if (start_at >= 0) {
pos++;
end_at = fio_atol(&pos);
if (end_at <= 0)
goto open_file;
}
/* we ignore multimple ranges, only responding with the first range. */
if (start_at < 0) {
if (0 - start_at < file_data.st_size) {
offset = file_data.st_size - start_at;
length = 0 - start_at;
}
} else if (end_at) {
offset = start_at;
length = end_at - start_at + 1;
if (length + start_at > file_data.st_size || length <= 0)
length = length - start_at;
} else {
offset = start_at;
length = length - start_at;
}
h->status = 206;
{
FIOBJ cranges = fiobj_str_buf(1);
fiobj_str_printf(cranges, "bytes %lu-%lu/%lu",
(unsigned long)start_at,
(unsigned long)(start_at + length - 1),
(unsigned long)file_data.st_size);
http_set_header(h, HTTP_HEADER_CONTENT_RANGE, cranges);
}
http_set_header(h, HTTP_HEADER_ACCEPT_RANGES,
fiobj_dup(HTTP_HVALUE_BYTES));
}
}
}
/* test for an OPTIONS request or invalid methods */
s = fiobj_obj2cstr(h->method);
switch (s.len) {
case 7:
if (!strncasecmp("options", s.data, 7)) {
http_set_header2(h, (fio_str_info_s){.data = (char *)"allow", .len = 5},
(fio_str_info_s){.data = (char *)"GET, HEAD", .len = 9});
h->status = 200;
http_finish(h);
return 0;
}
break;
case 3:
if (!strncasecmp("get", s.data, 3))
goto open_file;
break;
case 4:
if (!strncasecmp("head", s.data, 4)) {
http_set_header(h, HTTP_HEADER_CONTENT_LENGTH, fiobj_num_new(length));
http_finish(h);
return 0;
}
break;
}
http_send_error(h, 403);
return 0;
open_file:
s = fiobj_obj2cstr(filename);
file = open(s.data, O_RDONLY);
if (file == -1) {
FIO_LOG_ERROR("(HTTP) couldn't open file %s!\n", s.data);
perror(" ");
http_send_error(h, 500);
return 0;
}
{
FIOBJ tmp = 0;
uintptr_t pos = 0;
if (is_gz) {
http_set_header(h, HTTP_HEADER_CONTENT_ENCODING,
fiobj_dup(HTTP_HVALUE_GZIP));
pos = s.len - 4;
while (pos && s.data[pos] != '.')
pos--;
pos++; /* assuming, but that's fine. */
tmp = http_mimetype_find(s.data + pos, s.len - pos - 3);
} else {
pos = s.len - 1;
while (pos && s.data[pos] != '.')
pos--;
pos++; /* assuming, but that's fine. */
tmp = http_mimetype_find(s.data + pos, s.len - pos);
}
if (tmp)
http_set_header(h, HTTP_HEADER_CONTENT_TYPE, tmp);
}
http_sendfile(h, file, length, offset);
return 0;
} | /**
* Sends the response headers and the specified file (the response's body).
*
* Returns -1 eton error and 0 on success.
*
* AFTER THIS FUNCTION IS CALLED, THE `http_s` OBJECT IS NO LONGER VALID.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L389-L616 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_send_error | int http_send_error(http_s *r, size_t error) {
if (!r || !r->private_data.out_headers) {
return -1;
}
if (error < 100 || error >= 1000)
error = 500;
r->status = error;
char buffer[16];
buffer[0] = '/';
size_t pos = 1 + fio_ltoa(buffer + 1, error, 10);
buffer[pos++] = '.';
buffer[pos++] = 'h';
buffer[pos++] = 't';
buffer[pos++] = 'm';
buffer[pos++] = 'l';
buffer[pos] = 0;
if (http_sendfile2(r, http2protocol(r)->settings->public_folder,
http2protocol(r)->settings->public_folder_length, buffer,
pos)) {
http_set_header(r, HTTP_HEADER_CONTENT_TYPE,
http_mimetype_find((char *)"txt", 3));
fio_str_info_s t = http_status2str(error);
http_send_body(r, t.data, t.len);
}
return 0;
} | /**
* Sends an HTTP error response.
*
* Returns -1 on error and 0 on success.
*
* AFTER THIS FUNCTION IS CALLED, THE `http_s` OBJECT IS NO LONGER VALID.
*
* The `uuid` argument is optional and will be used only if the `http_s`
* argument is set to NULL.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L628-L653 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_finish | void http_finish(http_s *r) {
if (!r || !r->private_data.vtbl) {
return;
}
add_content_length(r, 0);
add_date(r);
((http_vtable_s *)r->private_data.vtbl)->http_finish(r);
} | /**
* Sends the response headers for a header only response.
*
* AFTER THIS FUNCTION IS CALLED, THE `http_s` OBJECT IS NO LONGER VALID.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L660-L667 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_push_data | int http_push_data(http_s *r, void *data, uintptr_t length, FIOBJ mime_type) {
if (!r || !(http_fio_protocol_s *)r->private_data.flag)
return -1;
return ((http_vtable_s *)r->private_data.vtbl)
->http_push_data(r, data, length, mime_type);
} | /**
* Pushes a data response when supported (HTTP/2 only).
*
* Returns -1 on error and 0 on success.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L673-L678 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_push_file | int http_push_file(http_s *h, FIOBJ filename, FIOBJ mime_type) {
if (HTTP_INVALID_HANDLE(h))
return -1;
return ((http_vtable_s *)h->private_data.vtbl)
->http_push_file(h, filename, mime_type);
} | /**
* Pushes a file response when supported (HTTP/2 only).
*
* If `mime_type` is NULL, an attempt at automatic detection using
* `filename` will be made.
*
* Returns -1 on error and 0 on success.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L687-L692 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_pause_wrapper | static void http_pause_wrapper(void *h_, void *task_) {
void (*task)(void *h) = (void (*)(void *h))((uintptr_t)task_);
task(h_);
} | /* perform the pause task outside of the connection's lock */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L737-L740 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_resume_wrapper | static void http_resume_wrapper(intptr_t uuid, fio_protocol_s *p_, void *arg) {
http_fio_protocol_s *p = (http_fio_protocol_s *)p_;
http_pause_handle_s *http = arg;
http_s *h = http->h;
h->udata = http->udata;
http_vtable_s *vtbl = (http_vtable_s *)h->private_data.vtbl;
if (http->task)
http->task(h);
vtbl->http_on_resume(h, p);
fio_free(http);
(void)uuid;
} | /* perform the resume task within of the connection's lock */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L743-L754 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_resume_fallback_wrapper | static void http_resume_fallback_wrapper(intptr_t uuid, void *arg) {
http_pause_handle_s *http = arg;
if (http->fallback)
http->fallback(http->udata);
fio_free(http);
(void)uuid;
} | /* perform the resume task fallback */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L757-L763 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_pause | void http_pause(http_s *h, void (*task)(http_pause_handle_s *http)) {
if (HTTP_INVALID_HANDLE(h)) {
return;
}
http_fio_protocol_s *p = (http_fio_protocol_s *)h->private_data.flag;
http_vtable_s *vtbl = (http_vtable_s *)h->private_data.vtbl;
http_pause_handle_s *http = fio_malloc(sizeof(*http));
*http = (http_pause_handle_s){
.uuid = p->uuid,
.h = h,
.udata = h->udata,
};
vtbl->http_on_pause(h, p);
fio_defer(http_pause_wrapper, http, (void *)((uintptr_t)task));
} | /**
* Defers the request / response handling for later.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L768-L782 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_resume | void http_resume(http_pause_handle_s *http, void (*task)(http_s *h),
void (*fallback)(void *udata)) {
if (!http)
return;
http->task = task;
http->fallback = fallback;
fio_defer_io_task(http->uuid, .udata = http, .type = FIO_PR_LOCK_TASK,
.task = http_resume_wrapper,
.fallback = http_resume_fallback_wrapper);
} | /**
* Defers the request / response handling for later.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L787-L796 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_hijack | intptr_t http_hijack(http_s *h, fio_str_info_s *leftover) {
if (!h)
return -1;
return ((http_vtable_s *)h->private_data.vtbl)->http_hijack(h, leftover);
} | /**
* Hijacks the socket away from the HTTP protocol and away from facil.io.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L801-L805 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_on_request_fallback | static void http_on_request_fallback(http_s *h) { http_send_error(h, 404); } | /* *****************************************************************************
Setting the default settings and allocating a persistent copy
***************************************************************************** */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L811-L811 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_peer_addr | fio_str_info_s http_peer_addr(http_s *h) {
return fio_peer_addr(((http_fio_protocol_s *)h->private_data.flag)->uuid);
} | /**
* Returns the direct address of the connected peer (likely an intermediary).
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L959-L961 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_on_close_client | static void http_on_close_client(intptr_t uuid, fio_protocol_s *protocol) {
http_fio_protocol_s *p = (http_fio_protocol_s *)protocol;
http_settings_s *set = p->settings;
void (**original)(intptr_t, fio_protocol_s *) =
(void (**)(intptr_t, fio_protocol_s *))(set + 1);
if (set->on_finish)
set->on_finish(set);
original[0](uuid, protocol);
http_settings_free(set);
} | /* *****************************************************************************
HTTP client connections
***************************************************************************** */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L967-L977 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | FIO_IGNORE_MACRO | intptr_t http_connect FIO_IGNORE_MACRO(const char *url,
const char *unix_address,
struct http_settings_s arg_settings) {
if (!arg_settings.on_response && !arg_settings.on_upgrade) {
FIO_LOG_ERROR("http_connect requires either an on_response "
" or an on_upgrade callback.\n");
errno = EINVAL;
goto on_error;
}
size_t len = 0, h_len = 0;
char *a = NULL, *p = NULL, *host = NULL;
uint8_t is_websocket = 0;
uint8_t is_secure = 0;
FIOBJ path = FIOBJ_INVALID;
if (!url && !unix_address) {
FIO_LOG_ERROR("http_connect requires a valid address.");
errno = EINVAL;
goto on_error;
}
if (url) {
fio_url_s u = fio_url_parse(url, strlen(url));
if (u.scheme.data &&
(u.scheme.len == 2 || (u.scheme.len == 3 && u.scheme.data[2] == 's')) &&
u.scheme.data[0] == 'w' && u.scheme.data[1] == 's') {
is_websocket = 1;
is_secure = (u.scheme.len == 3);
} else if (u.scheme.data &&
(u.scheme.len == 4 ||
(u.scheme.len == 5 && u.scheme.data[4] == 's')) &&
u.scheme.data[0] == 'h' && u.scheme.data[1] == 't' &&
u.scheme.data[2] == 't' && u.scheme.data[3] == 'p') {
is_secure = (u.scheme.len == 5);
}
if (is_secure && !arg_settings.tls) {
FIO_LOG_ERROR("Secure connections (%.*s) require a TLS object.",
(int)u.scheme.len, u.scheme.data);
errno = EINVAL;
goto on_error;
}
if (u.path.data) {
path = fiobj_str_new(
u.path.data, strlen(u.path.data)); /* copy query and target as well */
}
if (unix_address) {
a = (char *)unix_address;
h_len = len = strlen(a);
host = a;
} else {
if (!u.host.data) {
FIO_LOG_ERROR("http_connect requires a valid address.");
errno = EINVAL;
goto on_error;
}
/***** no more error handling, since memory is allocated *****/
/* copy address */
a = fio_malloc(u.host.len + 1);
memcpy(a, u.host.data, u.host.len);
a[u.host.len] = 0;
len = u.host.len;
/* copy port */
if (u.port.data) {
p = fio_malloc(u.port.len + 1);
memcpy(p, u.port.data, u.port.len);
p[u.port.len] = 0;
} else if (is_secure) {
p = fio_malloc(3 + 1);
memcpy(p, "443", 3);
p[3] = 0;
} else {
p = fio_malloc(2 + 1);
memcpy(p, "80", 2);
p[2] = 0;
}
}
if (u.host.data) {
host = u.host.data;
h_len = u.host.len;
}
}
/* set settings */
if (!arg_settings.timeout)
arg_settings.timeout = 30;
http_settings_s *settings = http_settings_new(arg_settings);
settings->is_client = 1;
// if (settings->tls) {
// fio_tls_alpn_add(settings->tls, "http/1.1", http_on_open_client_http1,
// NULL, NULL);
// }
if (!arg_settings.ws_timeout)
settings->ws_timeout = 0; /* allow server to dictate timeout */
if (!arg_settings.timeout)
settings->timeout = 0; /* allow server to dictate timeout */
http_s *h = fio_malloc(sizeof(*h));
FIO_ASSERT(h, "HTTP Client handler allocation failed");
http_s_new(h, 0, http1_vtable());
h->udata = arg_settings.udata;
h->status = 0;
h->path = path;
settings->udata = h;
settings->tls = arg_settings.tls;
if (host)
http_set_header2(h, (fio_str_info_s){.data = (char *)"host", .len = 4},
(fio_str_info_s){.data = host, .len = h_len});
intptr_t ret;
if (is_websocket) {
/* force HTTP/1.1 */
ret = fio_connect(.address = a, .port = p, .on_fail = http_on_client_failed,
.on_connect = http_on_open_client, .udata = settings,
.tls = arg_settings.tls);
(void)0;
} else {
/* Allow for any HTTP version */
ret = fio_connect(.address = a, .port = p, .on_fail = http_on_client_failed,
.on_connect = http_on_open_client, .udata = settings,
.tls = arg_settings.tls);
(void)0;
}
if (a != unix_address)
fio_free(a);
fio_free(p);
return ret;
on_error:
if (arg_settings.on_finish)
arg_settings.on_finish(&arg_settings);
return -1;
} | /* sublime text marker */
/**
* Connects to an HTTP server as a client.
*
* Upon a successful connection, the `on_response` callback is called with an
* empty `http_s*` handler (status == 0). Use the same API to set it's content
* and send the request to the server. The next`on_response` will contain the
* response.
*
* `address` should contain a full URL style address for the server. i.e.:
* "http:/www.example.com:8080/"
*
* Returns -1 on error and 0 on success. the `on_finish` callback is always
* called.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L1036-L1163 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_sse_copy2str | static inline void http_sse_copy2str(FIOBJ dest, char *prefix, size_t pre_len,
fio_str_info_s data) {
if (!data.len)
return;
const char *stop = data.data + data.len;
while (data.len) {
fiobj_str_write(dest, prefix, pre_len);
char *pos = data.data;
while (pos < stop && *pos != '\n' && *pos != '\r')
++pos;
fiobj_str_write(dest, data.data, (uintptr_t)(pos - data.data));
fiobj_str_write(dest, "\r\n", 2);
if (*pos == '\r')
++pos;
if (*pos == '\n')
++pos;
data.len -= (uintptr_t)(pos - data.data);
data.data = pos;
}
} | /* *****************************************************************************
EventSource Support (SSE)
Note:
* `http_sse_subscribe` and `http_sse_unsubscribe` are implemented in the
http_internal logical unit.
***************************************************************************** */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L1213-L1232 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_sse_on_message | static void http_sse_on_message(fio_msg_s *msg) {
http_sse_internal_s *sse = msg->udata1;
struct http_sse_subscribe_args *args = msg->udata2;
/* perform a callback */
fio_protocol_s *pr = fio_protocol_try_lock(sse->uuid, FIO_PR_LOCK_TASK);
if (!pr)
goto postpone;
args->on_message(&sse->sse, msg->channel, msg->msg, args->udata);
fio_protocol_unlock(pr, FIO_PR_LOCK_TASK);
return;
postpone:
if (errno == EBADF)
return;
fio_message_defer(msg);
return;
} | /** The on message callback. the `*msg` pointer is to a temporary object. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L1235-L1250 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_sse_on_unsubscribe | static void http_sse_on_unsubscribe(void *sse_, void *args_) {
http_sse_internal_s *sse = sse_;
struct http_sse_subscribe_args *args = args_;
if (args->on_unsubscribe)
args->on_unsubscribe(args->udata);
fio_free(args);
http_sse_try_free(sse);
} | /** An optional callback for when a subscription is fully canceled. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L1259-L1266 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_sse_subscribe | uintptr_t http_sse_subscribe(http_sse_s *sse_,
struct http_sse_subscribe_args args) {
http_sse_internal_s *sse = FIO_LS_EMBD_OBJ(http_sse_internal_s, sse, sse_);
if (sse->uuid == -1)
return 0;
if (!args.on_message)
args.on_message = http_sse_on_message__direct;
struct http_sse_subscribe_args *udata = fio_malloc(sizeof(*udata));
FIO_ASSERT_ALLOC(udata);
*udata = args;
fio_atomic_add(&sse->ref, 1);
subscription_s *sub =
fio_subscribe(.channel = args.channel, .on_message = http_sse_on_message,
.on_unsubscribe = http_sse_on_unsubscribe, .udata1 = sse,
.udata2 = udata, .match = args.match);
if (!sub)
return 0;
fio_lock(&sse->lock);
fio_ls_s *pos = fio_ls_push(&sse->subscriptions, sub);
fio_unlock(&sse->lock);
return (uintptr_t)pos;
} | /**
* Subscribes to a channel. See {struct http_sse_subscribe_args} for possible
* arguments.
*
* Returns a subscription ID on success and 0 on failure.
*
* All subscriptions are automatically revoked once the connection is closed.
*
* If the connections subscribes to the same channel more than once, messages
* will be merged. However, another subscription ID will be assigned, and two
* calls to {http_sse_unsubscribe} will be required in order to unregister from
* the channel.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L1283-L1306 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_sse_unsubscribe | void http_sse_unsubscribe(http_sse_s *sse_, uintptr_t subscription) {
if (!sse_ || !subscription)
return;
http_sse_internal_s *sse = FIO_LS_EMBD_OBJ(http_sse_internal_s, sse, sse_);
subscription_s *sub = (subscription_s *)((fio_ls_s *)subscription)->obj;
fio_lock(&sse->lock);
fio_ls_remove((fio_ls_s *)subscription);
fio_unlock(&sse->lock);
fio_unsubscribe(sub);
} | /**
* Cancels a subscription and invalidates the subscription object.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L1311-L1320 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_upgrade2sse | int http_upgrade2sse(http_s *h, http_sse_s sse) {
if (HTTP_INVALID_HANDLE(h)) {
if (sse.on_close)
sse.on_close(&sse);
return -1;
}
return ((http_vtable_s *)h->private_data.vtbl)->http_upgrade2sse(h, &sse);
} | /**
* Upgrades an HTTP connection to an EventSource (SSE) connection.
*
* Thie `http_s` handle will be invalid after this call.
*
* On HTTP/1.1 connections, this will preclude future requests using the same
* connection.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L1331-L1338 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_sse_set_timout | void http_sse_set_timout(http_sse_s *sse_, uint8_t timeout) {
if (!sse_)
return;
http_sse_internal_s *sse = FIO_LS_EMBD_OBJ(http_sse_internal_s, sse, sse_);
fio_timeout_set(sse->uuid, timeout);
} | /**
* Sets the ping interval for SSE connections.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L1343-L1348 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_sse_write | int http_sse_write(http_sse_s *sse, struct http_sse_write_args args) {
if (!sse || !(args.id.len + args.data.len + args.event.len) ||
fio_is_closed(FIO_LS_EMBD_OBJ(http_sse_internal_s, sse, sse)->uuid))
return -1;
FIOBJ buf;
{
/* best guess at data length, ignoring missing fields and multiline data */
const size_t total = 4 + args.id.len + 2 + 7 + args.event.len + 2 + 6 +
args.data.len + 2 + 7 + 10 + 4;
buf = fiobj_str_buf(total);
}
http_sse_copy2str(buf, (char *)"id: ", 4, args.id);
http_sse_copy2str(buf, (char *)"event: ", 7, args.event);
if (args.retry) {
FIOBJ i = fiobj_num_new(args.retry);
fiobj_str_write(buf, (char *)"retry: ", 7);
fiobj_str_join(buf, i);
fiobj_free(i);
}
http_sse_copy2str(buf, (char *)"data: ", 6, args.data);
fiobj_str_write(buf, "\r\n", 2);
return FIO_LS_EMBD_OBJ(http_sse_internal_s, sse, sse)
->vtable->http_sse_write(sse, buf);
} | /**
* Writes data to an EventSource (SSE) connection.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L1354-L1377 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_sse2uuid | intptr_t http_sse2uuid(http_sse_s *sse) {
if (!sse ||
fio_is_closed(FIO_LS_EMBD_OBJ(http_sse_internal_s, sse, sse)->uuid))
return -1;
return FIO_LS_EMBD_OBJ(http_sse_internal_s, sse, sse)->uuid;
} | /**
* Get the connection's UUID (for fio_defer and similar use cases).
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L1382-L1387 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_sse_close | int http_sse_close(http_sse_s *sse) {
if (!sse ||
fio_is_closed(FIO_LS_EMBD_OBJ(http_sse_internal_s, sse, sse)->uuid))
return -1;
return FIO_LS_EMBD_OBJ(http_sse_internal_s, sse, sse)
->vtable->http_sse_close(sse);
} | /**
* Closes an EventSource (SSE) connection.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L1392-L1398 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_sse_free | void http_sse_free(http_sse_s *sse) {
http_sse_try_free(FIO_LS_EMBD_OBJ(http_sse_internal_s, sse, sse));
} | /**
* Frees an SSE handle by reference (decreases the reference count).
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L1413-L1415 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_urlstr2fiobj | static inline FIOBJ http_urlstr2fiobj(char *s, size_t len) {
FIOBJ o = fiobj_str_buf(len);
ssize_t l = http_decode_url(fiobj_obj2cstr(o).data, s, len);
if (l < 0) {
fiobj_free(o);
return fiobj_str_new(NULL, 0); /* empty string */
}
fiobj_str_resize(o, (size_t)l);
return o;
} | /* *****************************************************************************
HTTP GET and POST parsing helpers
***************************************************************************** */
/** URL decodes a string, returning a `FIOBJ`. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L1422-L1431 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_str2fiobj | static inline FIOBJ http_str2fiobj(char *s, size_t len, uint8_t encoded) {
switch (len) {
case 0:
return fiobj_str_new(NULL, 0); /* empty string */
case 4:
if (!strncasecmp(s, "true", 4))
return fiobj_true();
if (!strncasecmp(s, "null", 4))
return fiobj_null();
break;
case 5:
if (!strncasecmp(s, "false", 5))
return fiobj_false();
}
{
char *end = s;
const uint64_t tmp = fio_atol(&end);
if (end == s + len)
return fiobj_num_new(tmp);
}
{
char *end = s;
const double tmp = fio_atof(&end);
if (end == s + len)
return fiobj_float_new(tmp);
}
if (encoded)
return http_urlstr2fiobj(s, len);
return fiobj_str_new(s, len);
} | /** converts a string into a `FIOBJ`. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L1434-L1463 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_parse_query | void http_parse_query(http_s *h) {
if (!h->query)
return;
if (!h->params)
h->params = fiobj_hash_new();
fio_str_info_s q = fiobj_obj2cstr(h->query);
do {
char *cut = memchr(q.data, '&', q.len);
if (!cut)
cut = q.data + q.len;
char *cut2 = memchr(q.data, '=', (cut - q.data));
if (cut2) {
/* we only add named elements... */
http_add2hash(h->params, q.data, (size_t)(cut2 - q.data), (cut2 + 1),
(size_t)(cut - (cut2 + 1)), 1);
}
if (cut[0] == '&') {
/* protecting against some ...less informed... clients */
if (cut[1] == 'a' && cut[2] == 'm' && cut[3] == 'p' && cut[4] == ';')
cut += 5;
else
cut += 1;
}
q.len -= (uintptr_t)(cut - q.data);
q.data = cut;
} while (q.len);
} | /** Parses the query part of an HTTP request/response. Uses `http_add2hash`. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L1466-L1492 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_parse_cookies | void http_parse_cookies(http_s *h, uint8_t is_url_encoded) {
if (!h->headers)
return;
if (h->cookies && fiobj_hash_count(h->cookies)) {
FIO_LOG_WARNING("(http) attempting to parse cookies more than once.");
return;
}
static uint64_t setcookie_header_hash;
if (!setcookie_header_hash)
setcookie_header_hash = fiobj_obj2hash(HTTP_HEADER_SET_COOKIE);
FIOBJ c = fiobj_hash_get2(h->headers, fiobj_obj2hash(HTTP_HEADER_COOKIE));
if (c) {
if (!h->cookies)
h->cookies = fiobj_hash_new();
if (FIOBJ_TYPE_IS(c, FIOBJ_T_ARRAY)) {
/* Array of Strings */
size_t count = fiobj_ary_count(c);
for (size_t i = 0; i < count; ++i) {
http_parse_cookies_cookie_str(
h->cookies, fiobj_ary_index(c, (int64_t)i), is_url_encoded);
}
} else {
/* single string */
http_parse_cookies_cookie_str(h->cookies, c, is_url_encoded);
}
}
c = fiobj_hash_get2(h->headers, fiobj_obj2hash(HTTP_HEADER_SET_COOKIE));
if (c) {
if (!h->cookies)
h->cookies = fiobj_hash_new();
if (FIOBJ_TYPE_IS(c, FIOBJ_T_ARRAY)) {
/* Array of Strings */
size_t count = fiobj_ary_count(c);
for (size_t i = 0; i < count; ++i) {
http_parse_cookies_setcookie_str(
h->cookies, fiobj_ary_index(c, (int64_t)i), is_url_encoded);
}
} else {
/* single string */
http_parse_cookies_setcookie_str(h->cookies, c, is_url_encoded);
}
}
} | /** Parses any Cookie / Set-Cookie headers, using the `http_add2hash` scheme. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L1537-L1579 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_add2hash2 | int http_add2hash2(FIOBJ dest, char *name, size_t name_len, FIOBJ val,
uint8_t encoded) {
if (!name)
goto error;
FIOBJ nested_ary = FIOBJ_INVALID;
char *cut1;
/* we can't start with an empty object name */
while (name_len && name[0] == '[') {
--name_len;
++name;
}
if (!name_len) {
/* an empty name is an error */
goto error;
}
uint32_t nesting = ((uint32_t)~0);
rebase:
/* test for nesting level limit (limit at 32) */
if (!nesting)
goto error;
/* start clearing away bits. */
nesting >>= 1;
/* since we might be rebasing, notice that "name" might be "name]" */
cut1 = memchr(name, '[', name_len);
if (!cut1)
goto place_in_hash;
/* simple case "name=" (the "=" was already removed) */
if (cut1 == name) {
/* an empty name is an error */
goto error;
}
if (cut1 + 1 == name + name_len) {
/* we have name[= ... autocorrect */
name_len -= 1;
goto place_in_array;
}
if (cut1[1] == ']') {
/* Nested Array "name[]..." */
/* Test for name[]= */
if ((cut1 + 2) == name + name_len) {
name_len -= 2;
goto place_in_array;
}
/* Test for a nested Array format error */
if (cut1[2] != '[' || cut1[3] == ']') { /* error, we can't parse this */
goto error;
}
/* we have name[][key...= */
/* ensure array exists and it's an array + set nested_ary */
const size_t len = ((cut1[-1] == ']') ? (size_t)((cut1 - 1) - name)
: (size_t)(cut1 - name));
const uint64_t hash =
fiobj_hash_string(name, len); /* hash the current name */
nested_ary = fiobj_hash_get2(dest, hash);
if (!nested_ary) {
/* create a new nested array */
FIOBJ key =
encoded ? http_urlstr2fiobj(name, len) : fiobj_str_new(name, len);
nested_ary = fiobj_ary_new2(4);
fiobj_hash_set(dest, key, nested_ary);
fiobj_free(key);
} else if (!FIOBJ_TYPE_IS(nested_ary, FIOBJ_T_ARRAY)) {
/* convert existing object to an array - auto error correction */
FIOBJ key =
encoded ? http_urlstr2fiobj(name, len) : fiobj_str_new(name, len);
FIOBJ tmp = fiobj_ary_new2(4);
fiobj_ary_push(tmp, nested_ary);
nested_ary = tmp;
fiobj_hash_set(dest, key, nested_ary);
fiobj_free(key);
}
/* test if last object in the array is a hash - create hash if not */
dest = fiobj_ary_index(nested_ary, -1);
if (!dest || !FIOBJ_TYPE_IS(dest, FIOBJ_T_HASH)) {
dest = fiobj_hash_new();
fiobj_ary_push(nested_ary, dest);
}
/* rebase `name` to `key` and restart. */
cut1 += 3; /* consume "[][" */
name_len -= (size_t)(cut1 - name);
name = cut1;
goto rebase;
} else {
/* we have name[key]... */
const size_t len = ((cut1[-1] == ']') ? (size_t)((cut1 - 1) - name)
: (size_t)(cut1 - name));
const uint64_t hash =
fiobj_hash_string(name, len); /* hash the current name */
FIOBJ tmp = fiobj_hash_get2(dest, hash);
if (!tmp) {
/* hash doesn't exist, create it */
FIOBJ key =
encoded ? http_urlstr2fiobj(name, len) : fiobj_str_new(name, len);
tmp = fiobj_hash_new();
fiobj_hash_set(dest, key, tmp);
fiobj_free(key);
} else if (!FIOBJ_TYPE_IS(tmp, FIOBJ_T_HASH)) {
/* type error, referencing an existing object that isn't a Hash */
goto error;
}
dest = tmp;
/* no rollback is possible once we enter the new nesting level... */
nested_ary = FIOBJ_INVALID;
/* rebase `name` to `key` and restart. */
cut1 += 1; /* consume "[" */
name_len -= (size_t)(cut1 - name);
name = cut1;
goto rebase;
}
place_in_hash:
if (name[name_len - 1] == ']')
--name_len;
{
FIOBJ key = encoded ? http_urlstr2fiobj(name, name_len)
: fiobj_str_new(name, name_len);
FIOBJ old = fiobj_hash_replace(dest, key, val);
if (old) {
if (nested_ary) {
fiobj_hash_replace(dest, key, old);
old = fiobj_hash_new();
fiobj_hash_set(old, key, val);
fiobj_ary_push(nested_ary, old);
} else {
if (!FIOBJ_TYPE_IS(old, FIOBJ_T_ARRAY)) {
FIOBJ tmp = fiobj_ary_new2(4);
fiobj_ary_push(tmp, old);
old = tmp;
}
fiobj_ary_push(old, val);
fiobj_hash_replace(dest, key, old);
}
}
fiobj_free(key);
}
return 0;
place_in_array:
if (name[name_len - 1] == ']')
--name_len;
{
uint64_t hash = fiobj_hash_string(name, name_len);
FIOBJ ary = fiobj_hash_get2(dest, hash);
if (!ary) {
FIOBJ key = encoded ? http_urlstr2fiobj(name, name_len)
: fiobj_str_new(name, name_len);
ary = fiobj_ary_new2(4);
fiobj_hash_set(dest, key, ary);
fiobj_free(key);
} else if (!FIOBJ_TYPE_IS(ary, FIOBJ_T_ARRAY)) {
FIOBJ tmp = fiobj_ary_new2(4);
fiobj_ary_push(tmp, ary);
ary = tmp;
FIOBJ key = encoded ? http_urlstr2fiobj(name, name_len)
: fiobj_str_new(name, name_len);
fiobj_hash_replace(dest, key, ary);
fiobj_free(key);
}
fiobj_ary_push(ary, val);
}
return 0;
error:
fiobj_free(val);
errno = EOPNOTSUPP;
return -1;
} | /**
* Adds a named parameter to the hash, resolving nesting references.
*
* i.e.:
*
* * "name[]" references a nested Array (nested in the Hash).
* * "name[key]" references a nested Hash.
* * "name[][key]" references a nested Hash within an array. Hash keys will be
* unique (repeating a key advances the hash).
* * These rules can be nested (i.e. "name[][key1][][key2]...")
* * "name[][]" is an error (there's no way for the parser to analyze
* dimensions)
*
* Note: names can't begin with "[" or end with "]" as these are reserved
* characters.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L1597-L1770 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_add2hash | int http_add2hash(FIOBJ dest, char *name, size_t name_len, char *value,
size_t value_len, uint8_t encoded) {
return http_add2hash2(dest, name, name_len,
http_str2fiobj(value, value_len, encoded), encoded);
} | /**
* Adds a named parameter to the hash, resolving nesting references.
*
* i.e.:
*
* * "name[]" references a nested Array (nested in the Hash).
* * "name[key]" references a nested Hash.
* * "name[][key]" references a nested Hash within an array. Hash keys will be
* unique (repeating a key advances the hash).
* * These rules can be nested (i.e. "name[][key1][][key2]...")
* * "name[][]" is an error (there's no way for the parser to analyze
* dimensions)
*
* Note: names can't begin with "[" or end with "]" as these are reserved
* characters.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L1788-L1792 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_mime_parser_on_data | static void http_mime_parser_on_data(http_mime_parser_s *parser, void *name,
size_t name_len, void *filename,
size_t filename_len, void *mimetype,
size_t mimetype_len, void *value,
size_t value_len) {
if (!filename_len) {
http_add2hash(http_mime_parser2fio(parser)->h->params, name, name_len,
value, value_len, 0);
return;
}
FIOBJ n = fiobj_str_new(name, name_len);
fiobj_str_write(n, "[data]", 6);
fio_str_info_s tmp = fiobj_obj2cstr(n);
http_add2hash(http_mime_parser2fio(parser)->h->params, tmp.data, tmp.len,
value, value_len, 0);
fiobj_str_resize(n, name_len);
fiobj_str_write(n, "[name]", 6);
tmp = fiobj_obj2cstr(n);
http_add2hash(http_mime_parser2fio(parser)->h->params, tmp.data, tmp.len,
filename, filename_len, 0);
if (mimetype_len) {
fiobj_str_resize(n, name_len);
fiobj_str_write(n, "[type]", 6);
tmp = fiobj_obj2cstr(n);
http_add2hash(http_mime_parser2fio(parser)->h->params, tmp.data, tmp.len,
mimetype, mimetype_len, 0);
}
fiobj_free(n);
} | /** Called when all the data is available at once. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L1812-L1840 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_mime_parser_on_partial_start | static void http_mime_parser_on_partial_start(
http_mime_parser_s *parser, void *name, size_t name_len, void *filename,
size_t filename_len, void *mimetype, size_t mimetype_len) {
http_mime_parser2fio(parser)->partial_length = 0;
http_mime_parser2fio(parser)->partial_offset = 0;
http_mime_parser2fio(parser)->partial_name = fiobj_str_new(name, name_len);
if (!filename)
return;
fiobj_str_write(http_mime_parser2fio(parser)->partial_name, "[type]", 6);
fio_str_info_s tmp =
fiobj_obj2cstr(http_mime_parser2fio(parser)->partial_name);
http_add2hash(http_mime_parser2fio(parser)->h->params, tmp.data, tmp.len,
mimetype, mimetype_len, 0);
fiobj_str_resize(http_mime_parser2fio(parser)->partial_name, name_len);
fiobj_str_write(http_mime_parser2fio(parser)->partial_name, "[name]", 6);
tmp = fiobj_obj2cstr(http_mime_parser2fio(parser)->partial_name);
http_add2hash(http_mime_parser2fio(parser)->h->params, tmp.data, tmp.len,
filename, filename_len, 0);
fiobj_str_resize(http_mime_parser2fio(parser)->partial_name, name_len);
fiobj_str_write(http_mime_parser2fio(parser)->partial_name, "[data]", 6);
} | /** Called when the data didn't fit in the buffer. Data will be streamed. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L1843-L1867 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_mime_parser_on_partial_data | static void http_mime_parser_on_partial_data(http_mime_parser_s *parser,
void *value, size_t value_len) {
if (!http_mime_parser2fio(parser)->partial_offset)
http_mime_parser2fio(parser)->partial_offset =
http_mime_parser2fio(parser)->pos +
((uintptr_t)value -
(uintptr_t)http_mime_parser2fio(parser)->buffer.data);
http_mime_parser2fio(parser)->partial_length += value_len;
(void)value;
} | /** Called when partial data is available. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L1870-L1879 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_mime_parser_on_partial_end | static void http_mime_parser_on_partial_end(http_mime_parser_s *parser) {
fio_str_info_s tmp =
fiobj_obj2cstr(http_mime_parser2fio(parser)->partial_name);
FIOBJ o = FIOBJ_INVALID;
if (!http_mime_parser2fio(parser)->partial_length)
return;
if (http_mime_parser2fio(parser)->partial_length < 42) {
/* short data gets a new object */
o = fiobj_str_new(http_mime_parser2fio(parser)->buffer.data +
http_mime_parser2fio(parser)->partial_offset,
http_mime_parser2fio(parser)->partial_length);
} else {
/* longer data gets a reference object (memory collision concerns) */
o = fiobj_data_slice(http_mime_parser2fio(parser)->h->body,
http_mime_parser2fio(parser)->partial_offset,
http_mime_parser2fio(parser)->partial_length);
}
http_add2hash2(http_mime_parser2fio(parser)->h->params, tmp.data, tmp.len, o,
0);
fiobj_free(http_mime_parser2fio(parser)->partial_name);
http_mime_parser2fio(parser)->partial_name = FIOBJ_INVALID;
http_mime_parser2fio(parser)->partial_offset = 0;
} | /** Called when the partial data is complete. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L1882-L1905 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_mime_decode_url | static inline size_t http_mime_decode_url(char *dest, const char *encoded,
size_t length) {
return http_decode_url(dest, encoded, length);
} | /**
* Called when URL decoding is required.
*
* Should support inplace decoding (`dest == encoded`).
*
* Should return the length of the decoded string.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L1914-L1917 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_parse_body | int http_parse_body(http_s *h) {
static uint64_t content_type_hash;
if (!h->body)
return -1;
if (!content_type_hash)
content_type_hash = fiobj_hash_string("content-type", 12);
FIOBJ ct = fiobj_hash_get2(h->headers, content_type_hash);
fio_str_info_s content_type = fiobj_obj2cstr(ct);
if (content_type.len < 16)
return -1;
if (content_type.len >= 33 &&
!strncasecmp("application/x-www-form-urlencoded", content_type.data,
33)) {
if (!h->params)
h->params = fiobj_hash_new();
FIOBJ tmp = h->query;
h->query = h->body;
http_parse_query(h);
h->query = tmp;
return 0;
}
if (content_type.len >= 16 &&
!strncasecmp("application/json", content_type.data, 16)) {
content_type = fiobj_obj2cstr(h->body);
if (h->params)
return -1;
if (fiobj_json2obj(&h->params, content_type.data, content_type.len) == 0)
return -1;
if (FIOBJ_TYPE_IS(h->params, FIOBJ_T_HASH))
return 0;
FIOBJ tmp = h->params;
FIOBJ key = fiobj_str_new("JSON", 4);
h->params = fiobj_hash_new2(4);
fiobj_hash_set(h->params, key, tmp);
fiobj_free(key);
return 0;
}
http_fio_mime_s p = {.h = h};
if (http_mime_parser_init(&p.p, content_type.data, content_type.len))
return -1;
if (!h->params)
h->params = fiobj_hash_new();
do {
size_t cons = http_mime_parse(&p.p, p.buffer.data, p.buffer.len);
p.pos += cons;
p.buffer = fiobj_data_pread(h->body, p.pos, 4096);
} while (p.buffer.data && !p.p.done && !p.p.error);
fiobj_free(p.partial_name);
p.partial_name = FIOBJ_INVALID;
return 0;
} | /**
* Attempts to decode the request's body.
*
* Supported Types include:
* * application/x-www-form-urlencoded
* * application/json
* * multipart/form-data
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L1927-L1979 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_req2str | FIOBJ http_req2str(http_s *h) {
if (HTTP_INVALID_HANDLE(h) || !fiobj_hash_count(h->headers))
return FIOBJ_INVALID;
struct header_writer_s w;
w.dest = fiobj_str_buf(0);
if (h->status_str) {
fiobj_str_join(w.dest, h->version);
fiobj_str_write(w.dest, " ", 1);
fiobj_str_join(w.dest, fiobj_num_tmp(h->status));
fiobj_str_write(w.dest, " ", 1);
fiobj_str_join(w.dest, h->status_str);
fiobj_str_write(w.dest, "\r\n", 2);
} else {
fiobj_str_join(w.dest, h->method);
fiobj_str_write(w.dest, " ", 1);
fiobj_str_join(w.dest, h->path);
if (h->query) {
fiobj_str_write(w.dest, "?", 1);
fiobj_str_join(w.dest, h->query);
}
{
fio_str_info_s t = fiobj_obj2cstr(h->version);
if (t.len < 6 || t.data[5] != '1')
fiobj_str_write(w.dest, " HTTP/1.1\r\n", 10);
else {
fiobj_str_write(w.dest, " ", 1);
fiobj_str_join(w.dest, h->version);
fiobj_str_write(w.dest, "\r\n", 2);
}
}
}
fiobj_each1(h->headers, 0, write_header, &w);
fiobj_str_write(w.dest, "\r\n", 2);
if (h->body) {
// fiobj_data_seek(h->body, 0);
// fio_str_info_s t = fiobj_data_read(h->body, 0);
// fiobj_str_write(w.dest, t.data, t.len);
fiobj_str_join(w.dest, h->body);
}
return w.dest;
} | /* *****************************************************************************
HTTP Helper functions that could be used globally
***************************************************************************** */
/**
* Returns a String object representing the unparsed HTTP request (HTTP
* version is capped at HTTP/1.1). Mostly usable for proxy usage and
* debugging.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L1990-L2032 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_date2rfc2109 | size_t http_date2rfc2109(char *target, struct tm *tmbuf) {
/* note: day of month is always 2 digits */
char *pos = target;
uint16_t tmp;
pos[0] = DAY_NAMES[tmbuf->tm_wday][0];
pos[1] = DAY_NAMES[tmbuf->tm_wday][1];
pos[2] = DAY_NAMES[tmbuf->tm_wday][2];
pos[3] = ',';
pos[4] = ' ';
pos += 5;
tmp = tmbuf->tm_mday / 10;
pos[0] = '0' + tmp;
pos[1] = '0' + (tmbuf->tm_mday - (tmp * 10));
pos += 2;
*(pos++) = ' ';
pos[0] = MONTH_NAMES[tmbuf->tm_mon][0];
pos[1] = MONTH_NAMES[tmbuf->tm_mon][1];
pos[2] = MONTH_NAMES[tmbuf->tm_mon][2];
pos[3] = ' ';
pos += 4;
// write year.
pos += fio_ltoa(pos, tmbuf->tm_year + 1900, 10);
*(pos++) = ' ';
tmp = tmbuf->tm_hour / 10;
pos[0] = '0' + tmp;
pos[1] = '0' + (tmbuf->tm_hour - (tmp * 10));
pos[2] = ':';
tmp = tmbuf->tm_min / 10;
pos[3] = '0' + tmp;
pos[4] = '0' + (tmbuf->tm_min - (tmp * 10));
pos[5] = ':';
tmp = tmbuf->tm_sec / 10;
pos[6] = '0' + tmp;
pos[7] = '0' + (tmbuf->tm_sec - (tmp * 10));
pos += 8;
*pos++ = ' ';
*pos++ = '-';
*pos++ = '0';
*pos++ = '0';
*pos++ = '0';
*pos++ = '0';
*pos = 0;
return pos - target;
} | /* HTTP header format for Cookie ages */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L2293-L2336 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_time2str | size_t http_time2str(char *target, const time_t t) {
/* pre-print time every 1 or 2 seconds or so. */
static __thread time_t cached_tick;
static __thread char cached_httpdate[48];
static __thread size_t cached_len;
time_t last_tick = fio_last_tick().tv_sec;
if ((t | 7) < last_tick) {
/* this is a custom time, not "now", pass through */
struct tm tm;
http_gmtime(t, &tm);
return http_date2str(target, &tm);
}
if (last_tick > cached_tick) {
struct tm tm;
cached_tick = last_tick; /* refresh every second */
http_gmtime(last_tick, &tm);
cached_len = http_date2str(cached_httpdate, &tm);
}
memcpy(target, cached_httpdate, cached_len);
return cached_len;
} | /**
* Prints Unix time to a HTTP time formatted string.
*
* This variation implements cached results for faster processing, at the
* price of a less accurate string.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L2344-L2364 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_mimetype_register | void http_mimetype_register(char *file_ext, size_t file_ext_len,
FIOBJ mime_type_str) {
uintptr_t hash = FIO_HASH_FN(file_ext, file_ext_len, 0, 0);
if (mime_type_str == FIOBJ_INVALID) {
fio_mime_set_remove(&fio_http_mime_types, hash, FIOBJ_INVALID, NULL);
} else {
FIOBJ old = FIOBJ_INVALID;
fio_mime_set_overwrite(&fio_http_mime_types, hash, mime_type_str, &old);
if (old != FIOBJ_INVALID) {
FIO_LOG_WARNING("mime-type collision: %.*s was %s, now %s",
(int)file_ext_len, file_ext, fiobj_obj2cstr(old).data,
fiobj_obj2cstr(mime_type_str).data);
fiobj_free(old);
}
fiobj_free(mime_type_str); /* move ownership to the registry */
}
} | /** Registers a Mime-Type to be associated with the file extension. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L2491-L2507 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_mimetype_stats | void http_mimetype_stats(void) {
FIO_LOG_DEBUG("HTTP MIME hash storage count/capa: %zu / %zu",
fio_mime_set_count(&fio_http_mime_types),
fio_mime_set_capa(&fio_http_mime_types));
} | /** Registers a Mime-Type to be associated with the file extension. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L2510-L2514 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_mimetype_find | FIOBJ http_mimetype_find(char *file_ext, size_t file_ext_len) {
uintptr_t hash = FIO_HASH_FN(file_ext, file_ext_len, 0, 0);
return fiobj_dup(
fio_mime_set_find(&fio_http_mime_types, hash, FIOBJ_INVALID));
} | /**
* Finds the mime-type associated with the file extension.
* Remember to call `fiobj_free`.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L2520-L2524 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_mimetype_find2 | FIOBJ http_mimetype_find2(FIOBJ url) {
static __thread char buffer[LONGEST_FILE_EXTENSION_LENGTH + 1];
fio_str_info_s ext = {.data = NULL};
FIOBJ mimetype;
if (!url)
goto finish;
fio_str_info_s tmp = fiobj_obj2cstr(url);
uint8_t steps = 1;
while (tmp.len > steps || steps >= LONGEST_FILE_EXTENSION_LENGTH) {
switch (tmp.data[tmp.len - steps]) {
case '.':
--steps;
if (steps) {
ext.len = steps;
ext.data = buffer;
buffer[steps] = 0;
for (size_t i = 1; i <= steps; ++i) {
buffer[steps - i] = tolower(tmp.data[tmp.len - i]);
}
}
/* fallthrough */
case '/':
goto finish;
break;
}
++steps;
}
finish:
mimetype = http_mimetype_find(ext.data, ext.len);
if (!mimetype)
mimetype = fiobj_dup(HTTP_HVALUE_CONTENT_TYPE_DEFAULT);
return mimetype;
} | /**
* Finds the mime-type associated with the URL.
* Remember to call `fiobj_free`.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L2530-L2562 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_mimetype_clear | void http_mimetype_clear(void) {
fio_mime_set_free(&fio_http_mime_types);
fiobj_free(current_date);
current_date = FIOBJ_INVALID;
last_date_added = 0;
} | /** Clears the Mime-Type registry (it will be empty afterthis call). */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L2565-L2570 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_status2str | fio_str_info_s http_status2str(uintptr_t status) {
static const fio_str_info_s status2str[] = {
HTTP_SET_STATUS_STR(100, "Continue"),
HTTP_SET_STATUS_STR(101, "Switching Protocols"),
HTTP_SET_STATUS_STR(102, "Processing"),
HTTP_SET_STATUS_STR(103, "Early Hints"),
HTTP_SET_STATUS_STR(200, "OK"),
HTTP_SET_STATUS_STR(201, "Created"),
HTTP_SET_STATUS_STR(202, "Accepted"),
HTTP_SET_STATUS_STR(203, "Non-Authoritative Information"),
HTTP_SET_STATUS_STR(204, "No Content"),
HTTP_SET_STATUS_STR(205, "Reset Content"),
HTTP_SET_STATUS_STR(206, "Partial Content"),
HTTP_SET_STATUS_STR(207, "Multi-Status"),
HTTP_SET_STATUS_STR(208, "Already Reported"),
HTTP_SET_STATUS_STR(226, "IM Used"),
HTTP_SET_STATUS_STR(300, "Multiple Choices"),
HTTP_SET_STATUS_STR(301, "Moved Permanently"),
HTTP_SET_STATUS_STR(302, "Found"),
HTTP_SET_STATUS_STR(303, "See Other"),
HTTP_SET_STATUS_STR(304, "Not Modified"),
HTTP_SET_STATUS_STR(305, "Use Proxy"),
HTTP_SET_STATUS_STR(306, "(Unused), "),
HTTP_SET_STATUS_STR(307, "Temporary Redirect"),
HTTP_SET_STATUS_STR(308, "Permanent Redirect"),
HTTP_SET_STATUS_STR(400, "Bad Request"),
HTTP_SET_STATUS_STR(403, "Forbidden"),
HTTP_SET_STATUS_STR(404, "Not Found"),
HTTP_SET_STATUS_STR(401, "Unauthorized"),
HTTP_SET_STATUS_STR(402, "Payment Required"),
HTTP_SET_STATUS_STR(405, "Method Not Allowed"),
HTTP_SET_STATUS_STR(406, "Not Acceptable"),
HTTP_SET_STATUS_STR(407, "Proxy Authentication Required"),
HTTP_SET_STATUS_STR(408, "Request Timeout"),
HTTP_SET_STATUS_STR(409, "Conflict"),
HTTP_SET_STATUS_STR(410, "Gone"),
HTTP_SET_STATUS_STR(411, "Length Required"),
HTTP_SET_STATUS_STR(412, "Precondition Failed"),
HTTP_SET_STATUS_STR(413, "Payload Too Large"),
HTTP_SET_STATUS_STR(414, "URI Too Long"),
HTTP_SET_STATUS_STR(415, "Unsupported Media Type"),
HTTP_SET_STATUS_STR(416, "Range Not Satisfiable"),
HTTP_SET_STATUS_STR(417, "Expectation Failed"),
HTTP_SET_STATUS_STR(421, "Misdirected Request"),
HTTP_SET_STATUS_STR(422, "Unprocessable Entity"),
HTTP_SET_STATUS_STR(423, "Locked"),
HTTP_SET_STATUS_STR(424, "Failed Dependency"),
HTTP_SET_STATUS_STR(425, "Unassigned"),
HTTP_SET_STATUS_STR(426, "Upgrade Required"),
HTTP_SET_STATUS_STR(427, "Unassigned"),
HTTP_SET_STATUS_STR(428, "Precondition Required"),
HTTP_SET_STATUS_STR(429, "Too Many Requests"),
HTTP_SET_STATUS_STR(430, "Unassigned"),
HTTP_SET_STATUS_STR(431, "Request Header Fields Too Large"),
HTTP_SET_STATUS_STR(500, "Internal Server Error"),
HTTP_SET_STATUS_STR(501, "Not Implemented"),
HTTP_SET_STATUS_STR(502, "Bad Gateway"),
HTTP_SET_STATUS_STR(503, "Service Unavailable"),
HTTP_SET_STATUS_STR(504, "Gateway Timeout"),
HTTP_SET_STATUS_STR(505, "HTTP Version Not Supported"),
HTTP_SET_STATUS_STR(506, "Variant Also Negotiates"),
HTTP_SET_STATUS_STR(507, "Insufficient Storage"),
HTTP_SET_STATUS_STR(508, "Loop Detected"),
HTTP_SET_STATUS_STR(509, "Unassigned"),
HTTP_SET_STATUS_STR(510, "Not Extended"),
HTTP_SET_STATUS_STR(511, "Network Authentication Required"),
};
fio_str_info_s ret = (fio_str_info_s){.len = 0, .data = NULL};
if (status >= 100 &&
(status - 100) < sizeof(status2str) / sizeof(status2str[0]))
ret = status2str[status - 100];
if (!ret.data) {
ret = status2str[400];
}
return ret;
} | // clang-format on
/** Returns the status as a C string struct */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http.c#L2616-L2691 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http1_after_finish | static inline void http1_after_finish(http_s *h) {
http1pr_s *p = handle2pr(h);
p->stop = p->stop & (~1UL);
if (h != &p->request) {
http_s_destroy(h, 0);
fio_free(h);
} else {
http_s_clear(h, p->p.settings->log);
}
if (p->close)
fio_close(p->p.uuid);
} | /* cleanup an HTTP/1.1 handler object */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http1.c#L51-L62 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http1_send_body | static int http1_send_body(http_s *h, void *data, uintptr_t length) {
FIOBJ packet = headers2str(h, length);
if (!packet) {
http1_after_finish(h);
return -1;
}
fiobj_str_write(packet, data, length);
fiobj_send_free((handle2pr(h)->p.uuid), packet);
http1_after_finish(h);
return 0;
} | /** Should send existing headers and data */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http1.c#L177-L188 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http1_sendfile | static int http1_sendfile(http_s *h, int fd, uintptr_t length,
uintptr_t offset) {
FIOBJ packet = headers2str(h, 0);
if (!packet) {
close(fd);
http1_after_finish(h);
return -1;
}
if (length < HTTP_MAX_HEADER_LENGTH) {
/* optimize away small files */
fio_str_info_s s = fiobj_obj2cstr(packet);
fiobj_str_capa_assert(packet, s.len + length);
s = fiobj_obj2cstr(packet);
intptr_t i = pread(fd, s.data + s.len, length, offset);
if (i < 0) {
close(fd);
fiobj_send_free((handle2pr(h)->p.uuid), packet);
fio_close((handle2pr(h)->p.uuid));
return -1;
}
close(fd);
fiobj_str_resize(packet, s.len + i);
fiobj_send_free((handle2pr(h)->p.uuid), packet);
http1_after_finish(h);
return 0;
}
fiobj_send_free((handle2pr(h)->p.uuid), packet);
fio_sendfile((handle2pr(h)->p.uuid), fd, offset, length);
http1_after_finish(h);
return 0;
} | /** Should send existing headers and file */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http1.c#L190-L220 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | htt1p_finish | static void htt1p_finish(http_s *h) {
FIOBJ packet = headers2str(h, 0);
if (packet)
fiobj_send_free((handle2pr(h)->p.uuid), packet);
else {
// fprintf(stderr, "WARNING: invalid call to `htt1p_finish`\n");
}
http1_after_finish(h);
} | /** Should send existing headers or complete streaming */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http1.c#L223-L231 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http1_push_data | static int http1_push_data(http_s *h, void *data, uintptr_t length,
FIOBJ mime_type) {
return -1;
(void)h;
(void)data;
(void)length;
(void)mime_type;
} | /** Push for data - unsupported. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http1.c#L233-L240 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http1_push_file | static int http1_push_file(http_s *h, FIOBJ filename, FIOBJ mime_type) {
return -1;
(void)h;
(void)filename;
(void)mime_type;
} | /** Push for files - unsupported. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http1.c#L242-L247 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http1_on_pause | static void http1_on_pause(http_s *h, http_fio_protocol_s *pr) {
((http1pr_s *)pr)->stop = 1;
fio_suspend(pr->uuid);
(void)h;
} | /**
* Called befor a pause task,
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http1.c#L252-L256 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http1_on_resume | static void http1_on_resume(http_s *h, http_fio_protocol_s *pr) {
if (!((http1pr_s *)pr)->stop) {
fio_force_event(pr->uuid, FIO_EVENT_ON_DATA);
}
(void)h;
} | /**
* called after the resume task had completed.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http1.c#L261-L266 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http1_websocket_client_on_upgrade | static void http1_websocket_client_on_upgrade(http_s *h, char *proto,
size_t len) {
http1pr_s *p = handle2pr(h);
websocket_settings_s *args = h->udata;
const intptr_t uuid = handle2pr(h)->p.uuid;
http_settings_s *set = handle2pr(h)->p.settings;
set->udata = NULL;
http_finish(h);
p->stop = 1;
websocket_attach(uuid, set, args, p->parser.state.next,
p->buf_len - (intptr_t)(p->parser.state.next - p->buf));
fio_free(args);
(void)proto;
(void)len;
} | /* *****************************************************************************
Websockets Upgrading
***************************************************************************** */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http1.c#L291-L305 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http1_upgrade2sse | static int http1_upgrade2sse(http_s *h, http_sse_s *sse) {
const intptr_t uuid = handle2pr(h)->p.uuid;
/* send response */
h->status = 200;
http_set_header(h, HTTP_HEADER_CONTENT_TYPE, fiobj_dup(HTTP_HVALUE_SSE_MIME));
http_set_header(h, HTTP_HEADER_CACHE_CONTROL,
fiobj_dup(HTTP_HVALUE_NO_CACHE));
http_set_header(h, HTTP_HEADER_CONTENT_ENCODING,
fiobj_str_new("identity", 8));
handle2pr(h)->stop = 1;
htt1p_finish(h); /* avoid the enforced content length in http_finish */
/* switch protocol to SSE */
http1_sse_fio_protocol_s *sse_pr = fio_malloc(sizeof(*sse_pr));
if (!sse_pr)
goto failed;
*sse_pr = (http1_sse_fio_protocol_s){
.p =
{
.on_ready = http1_sse_on_ready,
.on_shutdown = http1_sse_on_shutdown,
.on_close = http1_sse_on_close,
.ping = http1_sse_ping,
},
.sse = fio_malloc(sizeof(*(sse_pr->sse))),
};
if (!sse_pr->sse)
goto failed;
http_sse_init(sse_pr->sse, uuid, &HTTP1_VTABLE, sse);
fio_timeout_set(uuid, handle2pr(h)->p.settings->ws_timeout);
if (sse->on_open)
sse->on_open(&sse_pr->sse->sse);
fio_attach(uuid, &sse_pr->p);
return 0;
failed:
fio_close(handle2pr(h)->p.uuid);
if (sse->on_close)
sse->on_close(sse);
return -1;
(void)sse;
} | /**
* Upgrades an HTTP connection to an EventSource (SSE) connection.
*
* Thie `http_s` handle will be invalid after this call.
*
* On HTTP/1.1 connections, this will preclude future requests using the same
* connection.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http1.c#L463-L506 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http1_sse_write | static int http1_sse_write(http_sse_s *sse, FIOBJ str) {
return fiobj_send_free(((http_sse_internal_s *)sse)->uuid, str);
} | /**
* Writes data to an EventSource (SSE) connection.
*
* See the {struct http_sse_write_args} for possible named arguments.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http1.c#L514-L516 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http1_sse_close | static int http1_sse_close(http_sse_s *sse) {
fio_close(((http_sse_internal_s *)sse)->uuid);
return 0;
} | /**
* Closes an EventSource (SSE) connection.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http1.c#L521-L524 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http1_on_request | static int http1_on_request(http1_parser_s *parser) {
http1pr_s *p = parser2http(parser);
http_on_request_handler______internal(&http1_pr2handle(p), p->p.settings);
if (p->request.method && !p->stop)
http_finish(&p->request);
h1_reset(p);
return fio_is_closed(p->p.uuid);
} | /* *****************************************************************************
Parser Callbacks
***************************************************************************** */
/** called when a request was received. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http1.c#L551-L558 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http1_on_response | static int http1_on_response(http1_parser_s *parser) {
http1pr_s *p = parser2http(parser);
http_on_response_handler______internal(&http1_pr2handle(p), p->p.settings);
if (p->request.status_str && !p->stop)
http_finish(&p->request);
h1_reset(p);
return fio_is_closed(p->p.uuid);
} | /** called when a response was received. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http1.c#L560-L567 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http1_on_method | static int http1_on_method(http1_parser_s *parser, char *method,
size_t method_len) {
http1_pr2handle(parser2http(parser)).method =
fiobj_str_new(method, method_len);
parser2http(parser)->header_size += method_len;
return 0;
} | /** called when a request method is parsed. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http1.c#L569-L575 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http1_on_status | static int http1_on_status(http1_parser_s *parser, size_t status,
char *status_str, size_t len) {
http1_pr2handle(parser2http(parser)).status_str =
fiobj_str_new(status_str, len);
http1_pr2handle(parser2http(parser)).status = status;
parser2http(parser)->header_size += len;
return 0;
} | /** called when a response status is parsed. the status_str is the string
* without the prefixed numerical status indicator.*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http1.c#L579-L586 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.