#include "xz_http.h" #include #include #include #include #include #include int xz_parse_url(const char *url, const char *scheme, xz_url_t *out) { size_t prefix = strlen(scheme); if (!url || strncmp(url, scheme, prefix)) return -1; const char *host = url + prefix; const char *authority_end = host + strcspn(host, "/?#"); const char *colon = memchr(host, ':', authority_end - host); size_t n = (size_t)((colon ? colon : authority_end) - host); if (!n || n >= sizeof(out->host) || strchr(url, '#')) return -1; for (size_t i = 0; i < n; ++i) if (!isalnum((unsigned char)host[i]) && host[i] != '-' && host[i] != '.') return -1; memcpy(out->host, host, n); out->host[n] = 0; out->port = 443; if (colon) { char *end; long port = strtol(colon + 1, &end, 10); if (!isdigit((unsigned char)colon[1]) || end != authority_end || port < 1 || port > 65535) return -1; out->port = (unsigned)port; } for (const unsigned char *p = (const unsigned char *)authority_end; *p; ++p) if (*p <= 32 || *p >= 127) return -1; int len = snprintf(out->path, sizeof(out->path), "%s%s", *authority_end == '/' ? "" : "/", authority_end); return len < 0 || (size_t)len >= sizeof(out->path) ? -1 : 0; } static int read_line(xz_tls_t *tls, char *out, size_t capacity) { for (size_t i = 0; i + 1 < capacity; ++i) { if (xz_tls_read_exact(tls, out + i, 1) || !out[i]) return -1; if (out[i] == '\n') { if (!i || out[i - 1] != '\r') return -1; out[i - 1] = 0; return (int)(i - 1); } } return -1; } static int has_token(const char *value, const char *token) { while (*value) { value += strspn(value, " ,\t"); size_t n = strcspn(value, ", \t"); if (n == strlen(token) && !strncasecmp(value, token, n)) return 1; value += n; } return 0; } int xz_read_headers(xz_tls_t *tls, xz_response_t *out) { memset(out, 0, sizeof(*out)); out->length = -1; char line[1024]; if (read_line(tls, line, sizeof(line)) < 0 || sscanf(line, "HTTP/1.%*u %u", &out->status) != 1) return -1; for (int count = 0; count < 64; ++count) { int length = read_line(tls, line, sizeof(line)); if (length <= 0) return length; char *value = strchr(line, ':'); if (!value) return -1; *value++ = 0; value += strspn(value, " \t"); size_t vlen = strlen(value); while (vlen && (value[vlen-1] == ' ' || value[vlen-1] == '\t')) value[--vlen] = 0; if (!strcasecmp(line, "Content-Length")) { char *end; long n = strtol(value, &end, 10); if (!isdigit((unsigned char)*value) || *end || n < 0 || n > INT_MAX || (out->length >= 0 && out->length != n)) return -1; out->length = (int)n; } else if (!strcasecmp(line, "Transfer-Encoding")) { if (strcasecmp(value, "chunked")) return -1; out->chunked = 1; } else if (!strcasecmp(line, "Connection")) out->connection_upgrade |= has_token(value, "upgrade"); else if (!strcasecmp(line, "Upgrade")) out->websocket |= has_token(value, "websocket"); else if (!strcasecmp(line, "Sec-WebSocket-Accept")) { if (vlen >= sizeof(out->accept) || out->accept[0]) return -1; memcpy(out->accept, value, vlen + 1); } } return -1; } int xz_read_body(xz_tls_t *tls, const xz_response_t *r, char *out, size_t capacity) { if (!capacity) return -1; size_t used = 0; if (r->chunked) { for (;;) { char line[128]; if (read_line(tls, line, sizeof(line)) <= 0) return -1; char *end; unsigned long n = strtoul(line, &end, 16); if (!isxdigit((unsigned char)*line) || (*end && *end != ';') || n >= capacity - used) return -1; if (!n) break; if (xz_tls_read_exact(tls, out + used, n) || read_line(tls, line, sizeof(line)) != 0) return -1; used += n; } } else if (r->length >= 0) { used = (size_t)r->length; if (used >= capacity || xz_tls_read_exact(tls, out, used)) return -1; } else { while (used < capacity - 1) { int n = xz_tls_read(tls, out + used, capacity - used - 1); if (n < 0) return -1; if (!n) break; used += (size_t)n; } if (used == capacity - 1) return -1; } out[used] = 0; return (int)used; }