aboutsummaryrefslogtreecommitdiff
path: root/src/ispeek.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ispeek.c')
-rw-r--r--src/ispeek.c92
1 files changed, 15 insertions, 77 deletions
diff --git a/src/ispeek.c b/src/ispeek.c
index 95e1a4c..2c518cb 100644
--- a/src/ispeek.c
+++ b/src/ispeek.c
@@ -40,81 +40,26 @@ char *delim = ":";
int recursive;
long port;
-static char *
-make_url(const char *url, const char *path)
-{
- char *p;
- size_t len = strlen(url);
-
- while (len > 0 && url[len-1] == '/')
- --len;
-
- while (*path == '/')
- ++path;
-
- p = grecs_malloc(len + strlen(path) + 2);
- memcpy(p, url, len);
- p[len++] = '/';
- strcpy(p + len, path);
- return p;
-}
-
-static size_t
-acc_cb(void *ptr, size_t size, size_t nmemb, void *data)
-{
- size_t realsize = size * nmemb;
- struct grecs_txtacc *acc = data;
-
- grecs_txtacc_grow(acc, ptr, realsize);
-
- return realsize;
-}
+struct closure {
+ char *text;
+ CURL *curl;
+ struct grecs_txtacc *acc;
+ char **argv;
+};
static char *
read_from(const char *path, CURL *curl, struct grecs_txtacc *acc)
{
- CURLcode res;
- long http_resp;
- char *text;
char *url;
-
- url = make_url(base_url, path);
- curl_easy_setopt(curl, CURLOPT_URL, url);
-
- res = curl_easy_perform(curl);
- if (res != CURLE_OK)
- die(EX_UNAVAILABLE, "CURL: %s", curl_easy_strerror(res));
-
- res = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_resp);
- if (res != CURLE_OK)
- die(EX_UNAVAILABLE, "CURL: %s", curl_easy_strerror(res));
-
- grecs_txtacc_grow_char(acc, 0);
- text = grecs_txtacc_finish(acc, 0);
- switch (http_resp) {
- case 200:
- break;
+ url = path_concat(base_url, path);
- case 404:
- grecs_txtacc_free_string(acc, text);
+ if (instance_store_read(url, curl))
return NULL;
-
- default:
- die(EX_UNAVAILABLE, "CURL: got response %3d, url %s",
- http_resp, url);
- }
- free(url);
- return text;
+ grecs_txtacc_grow_char(acc, 0);
+ return grecs_txtacc_finish(acc, 0);
}
-struct closure {
- char *text;
- CURL *curl;
- struct grecs_txtacc *acc;
- char **argv;
-};
-
static void print_dir(const char *path, struct closure *cl);
static void
@@ -149,11 +94,11 @@ print_dir(const char *path, struct closure *cl)
printf("%s%s\n", path, ws.ws_wordv[i]);
if (recursive) {
if (isroot) {
- char *p = make_url(path, ws.ws_wordv[i]);
- url = make_url(p, "/");
+ char *p = path_concat(path, ws.ws_wordv[i]);
+ url = path_concat(p, "/");
free(p);
} else if (ws.ws_wordv[i][strlen(ws.ws_wordv[i]) - 1] == '/') {
- url = make_url(path, ws.ws_wordv[i]);
+ url = path_concat(path, ws.ws_wordv[i]);
} else
continue;
list(url, cl);
@@ -245,18 +190,11 @@ ispeek_do(char **argv)
const char *path = *argv++;
char *text;
- curl = curl_easy_init();
- if (!curl)
- die(EX_UNAVAILABLE, "curl_easy_init failed");
+ acc = grecs_txtacc_create();
+ curl = instance_store_curl_new(acc);
if (port)
curl_easy_setopt(curl, CURLOPT_PORT, (long) port);
- curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
eclat_set_curl_trace(curl, debug_level(0));
-
- acc = grecs_txtacc_create();
-
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, acc_cb);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, acc);
text = read_from(path, curl, acc);
if (text) {

Return to:

Send suggestions and report system problems to the System administrator.