From 7d5368ba0ea906aadfaf57488cd7fef304d5da70 Mon Sep 17 00:00:00 2001 From: Dima Krasner Date: Sat, 13 Apr 2024 11:57:10 +0300 Subject: [PATCH] cleanup --- finger.c | 2 +- gopher.c | 16 ++++++++-------- gophers.c | 2 +- gplaces.c | 32 ++++++++++++++++++-------------- guppy.c | 4 ++-- spartan.c | 4 ++-- titan.c | 2 +- 7 files changed, 33 insertions(+), 29 deletions(-) diff --git a/finger.c b/finger.c index df7ad05..e362d9b 100644 --- a/finger.c +++ b/finger.c @@ -49,4 +49,4 @@ static void *finger_download(const Selector *sel, URL *url, char **mime, Parser } -const Protocol finger = {"finger", "79", tcp_read, tcp_peek, socket_error, tcp_close, finger_download}; +const Protocol finger = {"finger", "79", tcp_read, tcp_peek, socket_error, tcp_close, finger_download, set_fragment}; diff --git a/gopher.c b/gopher.c index b7caeda..b894eab 100644 --- a/gopher.c +++ b/gopher.c @@ -58,17 +58,17 @@ 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, size_t skip) { static char buffer[1024 + 3]; /* path\r\n\0 */ - char *input = NULL, *query = NULL; + char *input = NULL, *fragment = NULL; const char *path, *end; if (url->path[0] == '/' && url->path[1] == '7') { - switch (curl_url_get(url->cu, CURLUPART_QUERY, &query, CURLU_URLDECODE)) { - case CURLUE_OK: input = query; break; - case CURLUE_NO_QUERY: break; + switch (curl_url_get(url->cu, CURLUPART_FRAGMENT, &fragment, 0)) { + case CURLUE_OK: input = fragment; break; + case CURLUE_NO_FRAGMENT: break; default: return NULL; } if (input == NULL) { - if (!ask || (input = bestline(color ? "\33[35mQuery>\33[0m " : "Query> ")) == NULL || !set_input(url, input)) return NULL; + if (!ask || (input = bestline(color ? "\33[35mQuery>\33[0m " : "Query> ")) == NULL || !set_fragment(url, input)) return NULL; if (interactive) { bestlineHistoryAdd(input); bestlineHistoryAdd(url->url); } *len = snprintf(buffer, sizeof(buffer), "%s\t%s\r\n", sel->rawurl + skip + strcspn(sel->rawurl + skip, "/") + 2, input); } else { @@ -76,8 +76,8 @@ static char *gopher_request(const Selector *sel, URL *url, int ask, int *len, si if ((end = strrchr(path, '?')) == NULL) *len = snprintf(buffer, sizeof(buffer), "%s\t%s\r\n", path, input); else *len = snprintf(buffer, sizeof(buffer), "%.*s\t%s\r\n", (int)(end - path), path, input); } - if (input != query) free(input); - curl_free(query); + if (input != fragment) free(input); + curl_free(fragment); } else if (url->path[0] == '/' && url->path[1] != '\0') *len = snprintf(buffer, sizeof(buffer), "%s\r\n", sel->rawurl + skip + strcspn(sel->rawurl + skip, "/") + 2); else *len = snprintf(buffer, sizeof(buffer), "%s\r\n", sel->rawurl + skip + strcspn(sel->rawurl + skip, "/")); @@ -144,4 +144,4 @@ static void *gopher_download(const Selector *sel, URL *url, char **mime, Parser } -const Protocol gopher = {"gopher", "70", tcp_read, tcp_peek, socket_error, tcp_close, gopher_download}; +const Protocol gopher = {"gopher", "70", tcp_read, tcp_peek, socket_error, tcp_close, gopher_download, set_fragment}; diff --git a/gophers.c b/gophers.c index 47f3e5f..3d65481 100644 --- a/gophers.c +++ b/gophers.c @@ -45,4 +45,4 @@ static void *gophers_download(const Selector *sel, URL *url, char **mime, Parser } -const Protocol gophers = {"gophers", "70", ssl_read, ssl_peek, ssl_error, ssl_close, gophers_download}; +const Protocol gophers = {"gophers", "70", ssl_read, ssl_peek, ssl_error, ssl_close, gophers_download, set_fragment}; diff --git a/gplaces.c b/gplaces.c index 84e5055..371b0af 100644 --- a/gplaces.c +++ b/gplaces.c @@ -82,6 +82,7 @@ typedef struct Protocol { int (*error)(const URL *, void *, int); void (*close)(void *); void *(*download)(const Selector *, URL *, char **mime, Parser *, unsigned int redirs, int ask); + int (*set_input)(URL *url, const char *input); } Protocol; struct Selector { @@ -291,21 +292,24 @@ static void free_selectors(SelectorList *list) { } -static int set_input(URL *url, const char *input) { +static int set_query(URL *url, const char *input) { char *query, *tmp; -#ifdef GPLACES_WITH_TITAN - if (url->proto == &titan && (curl_url_set(url->cu, CURLUPART_FRAGMENT, input, CURLU_NON_SUPPORT_SCHEME) != CURLUE_OK || curl_url_get(url->cu, CURLUPART_URL, &tmp, 0) != CURLUE_OK)) return 0; - else if (url->proto != &titan) { -#else - if (1) { -#endif - if ((query = curl_easy_escape(NULL, input, 0)) == NULL) return 0; - if (curl_url_set(url->cu, CURLUPART_QUERY, query, CURLU_NON_SUPPORT_SCHEME) != CURLUE_OK || curl_url_get(url->cu, CURLUPART_URL, &tmp, 0) != CURLUE_OK) { curl_free(query); return 0; } - curl_free(query); - } + if ((query = curl_easy_escape(NULL, input, 0)) == NULL) return 0; + if (curl_url_set(url->cu, CURLUPART_QUERY, query, CURLU_NON_SUPPORT_SCHEME) != CURLUE_OK || curl_url_get(url->cu, CURLUPART_URL, &tmp, 0) != CURLUE_OK) { curl_free(query); return 0; } + curl_free(query); + curl_free(url->url); url->url = tmp; + return 1; +} + + +#if defined(GPLACES_WITH_TITAN) || defined(GPLACES_WITH_GOPHER) || defined(GPLACES_WITH_GOPHERS) || defined(GPLACSE_WITH_FINGER) +static int set_fragment(URL *url, const char *input) { + char *tmp; + if (curl_url_set(url->cu, CURLUPART_FRAGMENT, input, CURLU_NON_SUPPORT_SCHEME) != CURLUE_OK || curl_url_get(url->cu, CURLUPART_URL, &tmp, 0) != CURLUE_OK) return 0; curl_free(url->url); url->url = tmp; return 1; } +#endif static int parse_url(URL *url, const char *rawurl, const char *from, const char *input) { @@ -368,7 +372,7 @@ static int parse_url(URL *url, const char *rawurl, const char *from, const char #endif } - if (input != NULL && input[0] != '\0' && !set_input(url, input)) return 0; + if (input != NULL && input[0] != '\0' && !url->proto->set_input(url, input)) return 0; else if ((input == NULL || input[0] == '\0') && curl_url_get(url->cu, CURLUPART_URL, &url->url, 0) != CURLUE_OK) return 0; if (!(file = (strcmp(url->scheme, "file")) == 0) && curl_url_get(url->cu, CURLUPART_HOST, &url->host, 0) != CURLUE_OK) return 0; @@ -1062,7 +1066,7 @@ static int ssl_download(URL *url, SSL **body, char **mime, int request(const URL if ((line = bestline(buffer)) == NULL) goto fail; if (data[1] != '1' && interactive) bestlineHistoryAdd(line); if (data[1] == '1') bestlineMaskModeDisable(); - if (!set_input(url, line)) { free(line); goto fail; } + if (!set_query(url, line)) { free(line); goto fail; } free(line); if (data[1] != '1' && interactive) bestlineHistoryAdd(url->url); break; @@ -1135,7 +1139,7 @@ static void *gemini_download(const Selector *sel, URL *url, char **mime, Parser } -const Protocol gemini = {"gemini", "1965", ssl_read, ssl_peek, ssl_error, ssl_close, gemini_download}; +const Protocol gemini = {"gemini", "1965", ssl_read, ssl_peek, ssl_error, ssl_close, gemini_download, set_query}; /*============================================================================*/ diff --git a/guppy.c b/guppy.c index b674153..ae5ab90 100644 --- a/guppy.c +++ b/guppy.c @@ -115,7 +115,7 @@ static int do_guppy_download(URL *url, GuppySocket *s, char **mime, int ask) { free(chunk); if ((input = bestline(prompt)) == NULL) return 4; if (interactive) bestlineHistoryAdd(input); - if (!set_input(url, input)) { free(input); return 4; } + if (!set_query(url, input)) { free(input); return 4; } free(input); if (interactive) bestlineHistoryAdd(url->url); } else if (chunk->buffer[0] == '3') { @@ -272,4 +272,4 @@ static int guppy_read(void *c, void *buffer, int length) { } -const Protocol guppy = {"guppy", "6775", guppy_read, NULL, socket_error, guppy_close, guppy_download}; +const Protocol guppy = {"guppy", "6775", guppy_read, NULL, socket_error, guppy_close, guppy_download, set_query}; diff --git a/spartan.c b/spartan.c index fef9e21..5b261a9 100644 --- a/spartan.c +++ b/spartan.c @@ -88,7 +88,7 @@ static void *spartan_download(const Selector *sel, URL *url, char **mime, Parser default: return NULL; } if (sel->prompt && (input == NULL || *input == '\0')) { - if (!ask || (input = bestline(color ? "\33[35mData>\33[0m " : "Data> ")) == NULL || !set_input(url, input)) goto fail; + if (!ask || (input = bestline(color ? "\33[35mData>\33[0m " : "Data> ")) == NULL || !set_query(url, input)) goto fail; if (interactive) { bestlineHistoryAdd(input); bestlineHistoryAdd(url->url); } } if (input != NULL) inputlen = strlen(input); @@ -112,4 +112,4 @@ static void *spartan_download(const Selector *sel, URL *url, char **mime, Parser } -const Protocol spartan = {"spartan", "300", tcp_read, tcp_peek, socket_error, tcp_close, spartan_download}; +const Protocol spartan = {"spartan", "300", tcp_read, tcp_peek, socket_error, tcp_close, spartan_download, set_query}; diff --git a/titan.c b/titan.c index 86820b7..c455f70 100644 --- a/titan.c +++ b/titan.c @@ -116,4 +116,4 @@ static void *titan_upload(const Selector *sel, URL *url, char **mime, Parser *pa } -const Protocol titan = {"titan", "1965", ssl_read, ssl_peek, ssl_error, ssl_close, titan_upload}; +const Protocol titan = {"titan", "1965", ssl_read, ssl_peek, ssl_error, ssl_close, titan_upload, set_fragment};