Skip to content

Commit

Permalink
cache Gopher search results
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed May 27, 2023
1 parent e1c8b53 commit 474590e
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions gopher.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,28 @@ static void parse_gophermap_line(char *line, int *pre, Selector **sel, SelectorL


/*============================================================================*/
static char *gopher_request(const Selector *sel, const URL *url, int ask, int *len, size_t skip) {
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;
char *input = NULL, *query = NULL;
const char *path, *end;

if (url->path[0] == '/' && url->path[1] == '7') {
if (!ask || (input = bestline(color ? "\33[35mQuery>\33[0m " : "Query> ")) == NULL) return NULL;
if (interactive) bestlineHistoryAdd(input);
*len = snprintf(buffer, sizeof(buffer), "%s\t%s\r\n", sel->rawurl + skip + strcspn(sel->rawurl + skip, "/") + 2, input);
free(input);
switch (curl_url_get(url->cu, CURLUPART_QUERY, &query, CURLU_URLDECODE)) {
case CURLUE_OK: input = query; break;
case CURLUE_NO_QUERY: 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 (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);
} 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

0 comments on commit 474590e

Please sign in to comment.