static void _test_buffer(mget_buffer_t *buf, const char *name) { char test[256]; int it; for (it = 0; it < (int)sizeof(test)-1; it++) { test[it] = 'a' + it % 26; test[it + 1] = 0; mget_buffer_strcpy(buf, test); mget_buffer_strcat(buf, test); if (!strncmp(buf->data, test, it + 1) && !strncmp(buf->data + it + 1, test, it + 1)) { ok++; } else { failed++; info_printf("test_buffer.1 '%s': [%d] got %s (expected %s%s)\n", name, it, buf->data, test, test); } mget_buffer_memcpy(buf, test, it + 1); mget_buffer_memcat(buf, test, it + 1); if (!strncmp(buf->data, test, it + 1) && !strncmp(buf->data + it + 1, test, it + 1)) { ok++; } else { failed++; info_printf("test_buffer.2 '%s': [%d] got %s (expected %s%s)\n", name, it, buf->data, test, test); } mget_buffer_printf(buf, "%s%s", test, test); if (!strncmp(buf->data, test, it + 1) && !strncmp(buf->data + it + 1, test, it + 1)) { ok++; } else { failed++; info_printf("test_buffer.3 '%s': [%d] got %s (expected %s%s)\n", name, it, buf->data, test, test); } mget_buffer_printf(buf, "%s", test); mget_buffer_printf_append(buf, "%s", test); if (!strncmp(buf->data, test, it + 1) && !strncmp(buf->data + it + 1, test, it + 1)) { ok++; } else { failed++; info_printf("test_buffer.4 '%s': [%d] got %s (expected %s%s)\n", name, it, buf->data, test, test); } } }
size_t mget_buffer_bufcpy(mget_buffer_t *buf, mget_buffer_t *src) { return mget_buffer_memcpy(buf, src->data, src->length); }
//char *iri_relative_to_absolute(IRI *iri, const char *tag, const char *val, size_t len, char *dst, size_t dst_size) const char *mget_iri_relative_to_abs(mget_iri_t *base, const char *val, size_t len, mget_buffer_t *buf) { debug_printf("*url = %.*s\n", (int)len, val); if (*val == '/') { if (base) { char path[len + 1]; strlcpy(path, val, len + 1); if (len >= 2 && val[1] == '/') { char *p; // absolute URI without scheme: //authority/path... if ((p = strchr(path + 2, '/'))) _normalize_path(p + 1); mget_buffer_strcpy(buf, base->scheme); mget_buffer_strcat(buf, ":"); mget_buffer_strcat(buf, path); debug_printf("*1 %s\n", buf->data); } else { // absolute path _normalize_path(path); mget_buffer_strcpy(buf, mget_iri_get_connection_part(base)); mget_buffer_strcat(buf, "/"); mget_buffer_strcat(buf, path); debug_printf("*2 %s\n", buf->data); } } else return NULL; } else { // see if URI begins with a scheme: if (memchr(val, ':', len)) { // absolute URI if (buf) { mget_buffer_memcpy(buf, val, len); debug_printf("*3 %s\n", buf->data); } else { debug_printf("*3 %s\n", val); return val; } } else if (base) { // relative path const char *lastsep = base->path ? strrchr(base->path, '/') : NULL; mget_buffer_strcpy(buf, mget_iri_get_connection_part(base)); mget_buffer_strcat(buf, "/"); size_t tmp_len = buf->length; if (lastsep) mget_buffer_memcat(buf, base->path, lastsep - base->path + 1); if (len) mget_buffer_memcat(buf, val, len); buf->length = _normalize_path(buf->data + tmp_len) + tmp_len; debug_printf("*4 %s %zu\n", buf->data, buf->length); } else if (val[len] == 0) { return val; } else return NULL; } return buf->data; }