AmendHub

Download:

jcs

/

detritus

/

amendments

/

27

request: protocol->scheme


jcs made amendment 27 about 1 year ago
--- request.c Wed Nov 6 22:07:45 2024 +++ request.c Fri Nov 8 08:36:56 2024 @@ -37,31 +37,31 @@ static bool request_tls_read_plaintext(struct tls_requ struct browser *browser, request_tls_reader reader, void *cookie); struct URI * -parse_uri(char *uristr, char *restrict_protocol) +parse_uri(char *uristr, char *restrict_scheme) { - static char protocol[URI_MAX_PROTOCOL_LEN + 1]; + static char scheme[URI_MAX_SCHEME_LEN + 1]; static char hostname[URI_MAX_HOSTNAME_LEN + 1]; static char path[URI_MAX_PATH_LEN + 1]; static char str[URI_MAX_STR_LEN + 1]; struct URI *uri; char *data; - size_t protocol_len, hostname_len, path_len, str_len, size; + size_t scheme_len, hostname_len, path_len, str_len, size; short count; unsigned short port; - /* protocol://host/path */ + /* scheme://host/path */ if (count = 0, sscanf(uristr, - "%" STR(URI_MAX_PROTOCOL_LEN) "[^:]://%" + "%" STR(URI_MAX_SCHEME_LEN) "[^:]://%" STR(URI_MAX_HOSTNAME_LEN) "[^/]%" STR(URI_MAX_PATH_LEN) "[ -~]%n", /* %s stops at whitespace, use all vis */ - &protocol, &hostname, &path, &count) == 3 && count > 10) + &scheme, &hostname, &path, &count) == 3 && count > 10) goto parse_ok; - /* protocol://host/ */ + /* scheme://host/ */ if (count = 0, sscanf(uristr, - "%" STR(URI_MAX_PROTOCOL_LEN) "[^:]://%" + "%" STR(URI_MAX_SCHEME_LEN) "[^:]://%" STR(URI_MAX_HOSTNAME_LEN) "[^/]/%n", - &protocol, &hostname, &count) == 2 && count > 10) { + &scheme, &hostname, &count) == 2 && count > 10) { path[0] = '/'; path[1] = '\0'; goto parse_ok; @@ -69,9 +69,9 @@ parse_uri(char *uristr, char *restrict_protocol) /* gemini://host */ if (count = 0, sscanf(uristr, - "%" STR(URI_MAX_PROTOCOL_LEN) "[^:]://%" + "%" STR(URI_MAX_SCHEME_LEN) "[^:]://%" STR(URI_MAX_HOSTNAME_LEN) "[^/]%n", - &protocol, &hostname, &count) == 2 && count > 10) { + &scheme, &hostname, &count) == 2 && count > 10) { path[0] = '/'; path[1] = '\0'; goto parse_ok; @@ -81,16 +81,15 @@ parse_uri(char *uristr, char *restrict_protocol) return NULL; parse_ok: - if (restrict_protocol != NULL && - strcasecmp(restrict_protocol, protocol) != 0) + if (restrict_scheme != NULL && strcasecmp(restrict_scheme, scheme) != 0) return NULL; - protocol_len = strlen(protocol); + scheme_len = strlen(scheme); hostname_len = strlen(hostname); path_len = strlen(path); - str_len = protocol_len + 3 + hostname_len + path_len; + str_len = scheme_len + 3 + hostname_len + path_len; - size = sizeof(struct URI) + protocol_len + 1 + hostname_len + 1 + + size = sizeof(struct URI) + scheme_len + 1 + hostname_len + 1 + path_len + 1 + str_len + 1; uri = xmalloc(size); if (uri == NULL) @@ -100,10 +99,10 @@ parse_ok: data = (char *)uri + sizeof(struct URI); - uri->protocol = data; - memcpy(uri->protocol, protocol, protocol_len); - uri->protocol[protocol_len] = '\0'; - data += protocol_len + 1; + uri->scheme = data; + memcpy(uri->scheme, scheme, scheme_len); + uri->scheme[scheme_len] = '\0'; + data += scheme_len + 1; uri->hostname = data; memcpy(uri->hostname, hostname, hostname_len); @@ -118,7 +117,7 @@ parse_ok: uri->str = data; data += snprintf(uri->str, str_len + 1, "%s://%s%s", - uri->protocol, uri->hostname, uri->path); + uri->scheme, uri->hostname, uri->path); data++; if (data > (char *)uri + size) @@ -136,11 +135,12 @@ build_relative_uri(struct URI *uri, char *relative, si /* http://a.b/c + //d/e -> http://d/e */ if (relative[0] == '/' && relative[1] == '/') { - /* retain protocol, new host and path */ - slen = snprintf(str, sizeof(str), "%s://", uri->protocol); + /* retain scheme, new host and path */ + slen = snprintf(str, sizeof(str), "%s://", uri->scheme); while (slen < sizeof(str) - 1 && len) { str[slen - 1] = *relative++; slen++; + len--; } str[slen] = '\0'; @@ -149,12 +149,13 @@ build_relative_uri(struct URI *uri, char *relative, si /* http://a.b/c + /d/e -> http://a.b/d/e */ if (relative[0] == '/') { - /* retain protocol and host, new path */ - slen = snprintf(str, sizeof(str), "%s://%s", uri->protocol, + /* retain scheme and host, new path */ + slen = snprintf(str, sizeof(str), "%s://%s", uri->scheme, uri->hostname); while (slen < sizeof(str) - 1 && len) { str[slen] = *relative++; slen++; + len--; } str[slen] = '\0'; @@ -165,7 +166,7 @@ build_relative_uri(struct URI *uri, char *relative, si /* http://a.b/c + gemini://d/e -> gemini://d/e */ if (n < len - 2 && relative[n] == ':' && relative[n + 1] == '/' && relative[n + 2] == '/') { - /* protocol found, this isn't relative */ + /* scheme found, this isn't relative */ if (len >= sizeof(str)) len = sizeof(str) - 1; memcpy(str, relative, len); @@ -193,7 +194,7 @@ build_relative_uri(struct URI *uri, char *relative, si memcpy(path + plen, relative, len); path[plen + len] = '\0'; - snprintf(str, sizeof(str), "%s://%s%s", uri->protocol, + snprintf(str, sizeof(str), "%s://%s%s", uri->scheme, uri->hostname, path); return parse_uri(str, NULL); } @@ -351,7 +352,7 @@ request_tls_connect(struct tls_request *request, struc return false; browser_statusf(browser, - "Connected to %s, performing TLS negotiation...", hostname); + "Connected to %s, negotiating TLS...", hostname); memset(&tls_req, 0, sizeof(tls_req)); strlcpy(tls_req.hostname, hostname, sizeof(tls_req.hostname)); @@ -368,7 +369,7 @@ request_tls_connect(struct tls_request *request, struc request->tls_id = scsi_tls_init(&tls_req); if (request->tls_id == 0) { - browser_statusf(browser, "TLS handshake failed!"); + browser_statusf(browser, "Error: TLS handshake failed!"); request_tcp_cleanup(&request->tcp); return false; } @@ -446,11 +447,14 @@ request_tls_shuffle(struct tls_request *request, struc continue; if (status) { - browser_statusf(browser, "TLS status is 0x%x?", status); + browser_statusf(browser, "Error: TLS status is 0x%x?", status); return false; } } + if (request->tcp_done_reading && request->tcp.input_len == 0) + return false; + return true; } @@ -464,11 +468,11 @@ request_tls_read_tls_ciphertext(struct tls_request *re /* read ciphertext from TLS and send it out TCP */ if (request->tcp_done_reading) - return false; + return true; - if (request_tcp_avail(&request->tcp, browser) == -1) { + if (request_tcp_avail(&request->tcp, browser) < 0) { request->tcp_done_reading = true; - return false; + return true; } /* this will point buf to scsi's static buffer */ @@ -571,10 +575,10 @@ request_tls_send_plaintext(struct tls_request *request return true; if (request->tls_state == REQ_STATE_NEGOTIATING) { - browser_statusf(browser, "Negotiated, sending request..."); + //browser_statusf(browser, "Negotiated, sending request..."); request->tls_state = REQ_STATE_SENDING_REQUEST; } else if (request->tls_state != REQ_STATE_SENDING_REQUEST) { - browser_statusf(browser, "In bogus state (%d) instead of " + browser_statusf(browser, "Error: bogus state (%d) instead of " "SENDING_REQUEST, disconnecting", request->tls_state); return false; } --- request.h Wed Nov 6 22:02:37 2024 +++ request.h Thu Nov 7 09:03:15 2024 @@ -34,13 +34,13 @@ struct tls_init_request { }; struct URI { -#define URI_MAX_PROTOCOL_LEN 20 - char *protocol; +#define URI_MAX_SCHEME_LEN 20 + char *scheme; #define URI_MAX_HOSTNAME_LEN 255 char *hostname; #define URI_MAX_PATH_LEN 512 char *path; -#define URI_MAX_STR_LEN (URI_MAX_PROTOCOL_LEN + 3 + URI_MAX_HOSTNAME_LEN + \ +#define URI_MAX_STR_LEN (URI_MAX_SCHEME_LEN + 3 + URI_MAX_HOSTNAME_LEN + \ URI_MAX_PATH_LEN) char *str; unsigned short port; @@ -64,7 +64,7 @@ struct tls_request { bool tcp_done_reading; }; -struct URI * parse_uri(char *uristr, char *restrict_protocol); +struct URI * parse_uri(char *uristr, char *restrict_scheme); struct URI * build_relative_uri(struct URI *uri, char *relative, size_t len); /* tcp functions */