diff options
| author | 2025-05-29 18:03:23 +0200 | |
|---|---|---|
| committer | 2025-05-29 18:03:23 +0200 | |
| commit | e030fe6c5054c0a9b76a55adc80bb81d7a73fcc3 (patch) | |
| tree | 2de5fa7ff075a76a73c5d9cdb036c027dbc411af | |
| parent | Fixed typo in rss_to_timeline(). (diff) | |
| download | snac2-e030fe6c5054c0a9b76a55adc80bb81d7a73fcc3.tar.gz snac2-e030fe6c5054c0a9b76a55adc80bb81d7a73fcc3.tar.xz snac2-e030fe6c5054c0a9b76a55adc80bb81d7a73fcc3.zip | |
Use HTTP caching (etag / if-none-match) in RSS downloads.
Diffstat (limited to '')
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | Makefile.NetBSD | 2 | ||||
| -rw-r--r-- | rss.c | 40 |
3 files changed, 40 insertions, 4 deletions
| @@ -68,7 +68,7 @@ mastoapi.o: mastoapi.c xs.h xs_hex.h xs_openssl.h xs_json.h xs_io.h \ | |||
| 68 | xs_time.h xs_glob.h xs_set.h xs_random.h xs_url.h xs_mime.h xs_match.h \ | 68 | xs_time.h xs_glob.h xs_set.h xs_random.h xs_url.h xs_mime.h xs_match.h \ |
| 69 | xs_unicode.h snac.h http_codes.h | 69 | xs_unicode.h snac.h http_codes.h |
| 70 | rss.o: rss.c xs.h xs_html.h xs_regex.h xs_time.h xs_match.h xs_curl.h \ | 70 | rss.o: rss.c xs.h xs_html.h xs_regex.h xs_time.h xs_match.h xs_curl.h \ |
| 71 | snac.h http_codes.h | 71 | xs_openssl.h xs_json.h snac.h http_codes.h |
| 72 | sandbox.o: sandbox.c xs.h snac.h http_codes.h | 72 | sandbox.o: sandbox.c xs.h snac.h http_codes.h |
| 73 | snac.o: snac.c xs.h xs_hex.h xs_io.h xs_unicode_tbl.h xs_unicode.h \ | 73 | snac.o: snac.c xs.h xs_hex.h xs_io.h xs_unicode_tbl.h xs_unicode.h \ |
| 74 | xs_json.h xs_curl.h xs_openssl.h xs_socket.h xs_unix_socket.h xs_url.h \ | 74 | xs_json.h xs_curl.h xs_openssl.h xs_socket.h xs_unix_socket.h xs_url.h \ |
diff --git a/Makefile.NetBSD b/Makefile.NetBSD index 9dc534e..d19ab2d 100644 --- a/Makefile.NetBSD +++ b/Makefile.NetBSD | |||
| @@ -57,7 +57,7 @@ mastoapi.o: mastoapi.c xs.h xs_hex.h xs_openssl.h xs_json.h xs_io.h \ | |||
| 57 | xs_time.h xs_glob.h xs_set.h xs_random.h xs_url.h xs_mime.h xs_match.h \ | 57 | xs_time.h xs_glob.h xs_set.h xs_random.h xs_url.h xs_mime.h xs_match.h \ |
| 58 | xs_unicode.h snac.h http_codes.h | 58 | xs_unicode.h snac.h http_codes.h |
| 59 | rss.o: rss.c xs.h xs_html.h xs_regex.h xs_time.h xs_match.h xs_curl.h \ | 59 | rss.o: rss.c xs.h xs_html.h xs_regex.h xs_time.h xs_match.h xs_curl.h \ |
| 60 | snac.h http_codes.h | 60 | xs_openssl.h xs_json.h snac.h http_codes.h |
| 61 | sandbox.o: sandbox.c xs.h snac.h http_codes.h | 61 | sandbox.o: sandbox.c xs.h snac.h http_codes.h |
| 62 | snac.o: snac.c xs.h xs_hex.h xs_io.h xs_unicode_tbl.h xs_unicode.h \ | 62 | snac.o: snac.c xs.h xs_hex.h xs_io.h xs_unicode_tbl.h xs_unicode.h \ |
| 63 | xs_json.h xs_curl.h xs_openssl.h xs_socket.h xs_unix_socket.h xs_url.h \ | 63 | xs_json.h xs_curl.h xs_openssl.h xs_socket.h xs_unix_socket.h xs_url.h \ |
| @@ -7,6 +7,8 @@ | |||
| 7 | #include "xs_time.h" | 7 | #include "xs_time.h" |
| 8 | #include "xs_match.h" | 8 | #include "xs_match.h" |
| 9 | #include "xs_curl.h" | 9 | #include "xs_curl.h" |
| 10 | #include "xs_openssl.h" | ||
| 11 | #include "xs_json.h" | ||
| 10 | 12 | ||
| 11 | #include "snac.h" | 13 | #include "snac.h" |
| 12 | 14 | ||
| @@ -117,12 +119,37 @@ void rss_to_timeline(snac *user, const char *url) | |||
| 117 | hdrs = xs_dict_set(hdrs, "accept", "application/rss+xml"); | 119 | hdrs = xs_dict_set(hdrs, "accept", "application/rss+xml"); |
| 118 | hdrs = xs_dict_set(hdrs, "user-agent", USER_AGENT); | 120 | hdrs = xs_dict_set(hdrs, "user-agent", USER_AGENT); |
| 119 | 121 | ||
| 122 | /* get the RSS metadata */ | ||
| 123 | xs *md5 = xs_md5_hex(url, strlen(url)); | ||
| 124 | xs *rss_md_fn = xs_fmt("%s/rss", user->basedir); | ||
| 125 | mkdirx(rss_md_fn); | ||
| 126 | rss_md_fn = xs_str_cat(rss_md_fn, "/", md5, ".json"); | ||
| 127 | |||
| 128 | xs *rss_md = NULL; | ||
| 129 | const char *etag = NULL; | ||
| 130 | |||
| 131 | FILE *f; | ||
| 132 | if ((f = fopen(rss_md_fn, "r")) != NULL) { | ||
| 133 | rss_md = xs_json_load(f); | ||
| 134 | fclose(f); | ||
| 135 | |||
| 136 | etag = xs_dict_get(rss_md, "etag"); | ||
| 137 | |||
| 138 | if (xs_is_string(etag)) | ||
| 139 | hdrs = xs_dict_set(hdrs, "if-none-match", etag); | ||
| 140 | } | ||
| 141 | |||
| 142 | if (rss_md == NULL) | ||
| 143 | rss_md = xs_dict_new(); | ||
| 144 | |||
| 120 | xs *payload = NULL; | 145 | xs *payload = NULL; |
| 121 | int status; | 146 | int status; |
| 122 | int p_size; | 147 | int p_size; |
| 123 | 148 | ||
| 124 | xs *rsp = xs_http_request("GET", url, hdrs, NULL, 0, &status, &payload, &p_size, 0); | 149 | xs *rsp = xs_http_request("GET", url, hdrs, NULL, 0, &status, &payload, &p_size, 0); |
| 125 | 150 | ||
| 151 | snac_log(user, xs_fmt("parsing RSS %s %d", url, status)); | ||
| 152 | |||
| 126 | if (!valid_status(status) || !xs_is_string(payload)) | 153 | if (!valid_status(status) || !xs_is_string(payload)) |
| 127 | return; | 154 | return; |
| 128 | 155 | ||
| @@ -131,8 +158,6 @@ void rss_to_timeline(snac *user, const char *url) | |||
| 131 | if (!xs_is_string(ctype) || xs_str_in(ctype, "application/rss+xml") == -1) | 158 | if (!xs_is_string(ctype) || xs_str_in(ctype, "application/rss+xml") == -1) |
| 132 | return; | 159 | return; |
| 133 | 160 | ||
| 134 | snac_log(user, xs_fmt("parsing RSS %s", url)); | ||
| 135 | |||
| 136 | /* yes, parsing is done with regexes (now I have two problems blah blah blah) */ | 161 | /* yes, parsing is done with regexes (now I have two problems blah blah blah) */ |
| 137 | xs *links = xs_regex_select(payload, "<link>[^<]+</link>"); | 162 | xs *links = xs_regex_select(payload, "<link>[^<]+</link>"); |
| 138 | const char *link; | 163 | const char *link; |
| @@ -208,6 +233,17 @@ void rss_to_timeline(snac *user, const char *url) | |||
| 208 | timeline_add(user, id, obj); | 233 | timeline_add(user, id, obj); |
| 209 | } | 234 | } |
| 210 | } | 235 | } |
| 236 | |||
| 237 | /* update the RSS metadata */ | ||
| 238 | etag = xs_dict_get(rsp, "etag"); | ||
| 239 | |||
| 240 | if (xs_is_string(etag)) { | ||
| 241 | rss_md = xs_dict_set(rss_md, "etag", etag); | ||
| 242 | if ((f = fopen(rss_md_fn, "w")) != NULL) { | ||
| 243 | xs_json_dump(rss_md, 4, f); | ||
| 244 | fclose(f); | ||
| 245 | } | ||
| 246 | } | ||
| 211 | } | 247 | } |
| 212 | 248 | ||
| 213 | 249 | ||