Skip to content

Commit

Permalink
fix bad request when selectors contain ?
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed May 2, 2023
1 parent 9e926ba commit 6f7221a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions gopher.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ static void parse_gophermap_line(char *line, int *pre, Selector **sel, SelectorL


/*============================================================================*/
static char *gopher_request(const Selector *sel, URL *url, int ask, int *len) {
static char *gopher_request(const Selector *sel, URL *url, int ask, int *len, size_t skip) {
static char buffer[1024 + 3]; /* path\r\n\0 */
char *query = NULL, *input = NULL;

Expand All @@ -72,8 +72,8 @@ static char *gopher_request(const Selector *sel, URL *url, int ask, int *len) {
if (interactive) { bestlineHistoryAdd(input); bestlineHistoryAdd(url->url); }
}
}
if (input && *input != '\0') *len = snprintf(buffer, sizeof(buffer), "%s\t%s\r\n", strncmp(url->path, "/7/", 3) == 0 ? url->path + 2 : url->path, input);
else *len = snprintf(buffer, sizeof(buffer), "%s\r\n", (url->path[1] != '/' && url->path[1] != '\0' && url->path[2] == '/') ? url->path + 2 : url->path);
if (input && *input != '\0') *len = snprintf(buffer, sizeof(buffer), "%s\t%s\r\n", strncmp(url->path, "/7/", 3) == 0 ? sel->rawurl + skip + strcspn(sel->rawurl + skip, "/") + 2 : sel->rawurl + skip + strcspn(sel->rawurl + skip, "/"), input);
else *len = snprintf(buffer, sizeof(buffer), "%s\r\n", (url->path[1] != '/' && url->path[1] != '\0' && url->path[2] == '/') ? sel->rawurl + skip + strcspn(sel->rawurl + skip, "/") + 2 : sel->rawurl + skip + strcspn(sel->rawurl + skip, "/"));

if (input != query) free(input);
curl_free(query);
Expand Down Expand Up @@ -124,7 +124,7 @@ static void *gopher_download(const Selector *sel, URL *url, char **mime, Parser
char *buffer;
int fd = -1, len;

if ((buffer = gopher_request(sel, url, ask, &len)) == NULL || (fd = tcp_connect(url)) == -1) goto fail;
if ((buffer = gopher_request(sel, url, ask, &len, 9)) == NULL || (fd = tcp_connect(url)) == -1) goto fail;
if (sendall(fd, buffer, len, MSG_NOSIGNAL) != len) {
if (errno == EAGAIN || errno == EWOULDBLOCK) error(0, "cannot send request to `%s`:`%s`: cancelled", url->host, url->port);
else error(0, "cannot send request to `%s`:`%s`: %s", url->host, url->port, strerror(errno));
Expand Down
2 changes: 1 addition & 1 deletion gophers.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static void *gophers_download(const Selector *sel, URL *url, char **mime, Parser
if ((ctx = SSL_CTX_new(TLS_client_method())) == NULL) return NULL;
SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, NULL);

if ((buffer = gopher_request(sel, url, ask, &len)) == NULL || (ssl = ssl_connect(url, ctx, ask)) == NULL) goto fail;
if ((buffer = gopher_request(sel, url, ask, &len, 10)) == NULL || (ssl = ssl_connect(url, ctx, ask)) == NULL) goto fail;
if ((err = SSL_get_error(ssl, SSL_write(ssl, buffer, len))) != SSL_ERROR_NONE) {
if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) error(0, "cannot send request to `%s`:`%s`: cancelled", url->host, url->port);
else error(0, "cannot send request to `%s`:`%s`: error %d", url->host, url->port, err);
Expand Down

0 comments on commit 6f7221a

Please sign in to comment.