Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Apr 13, 2024
1 parent 2827429 commit 7d5368b
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 29 deletions.
2 changes: 1 addition & 1 deletion finger.c
Original file line number Diff line number Diff line change
Expand Up @@ -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};
16 changes: 8 additions & 8 deletions gopher.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,26 @@ 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 {
path = sel->rawurl + skip + strcspn(sel->rawurl + skip, "/") + 2;
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, "/"));

Expand Down Expand Up @@ -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};
2 changes: 1 addition & 1 deletion gophers.c
Original file line number Diff line number Diff line change
Expand Up @@ -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};
32 changes: 18 additions & 14 deletions gplaces.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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};


/*============================================================================*/
Expand Down
4 changes: 2 additions & 2 deletions guppy.c
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -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};
4 changes: 2 additions & 2 deletions spartan.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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};
2 changes: 1 addition & 1 deletion titan.c
Original file line number Diff line number Diff line change
Expand Up @@ -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};

0 comments on commit 7d5368b

Please sign in to comment.