xs_webmention.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* copyright (c) 2025 grunfink et al. / MIT license */
  2. #ifndef _XS_WEBMENTION_H
  3. #define _XS_WEBMENTION_H
  4. int xs_webmention_send(const char *source, const char *target, const char *user_agent);
  5. #ifdef XS_IMPLEMENTATION
  6. int xs_webmention_send(const char *source, const char *target, const char *user_agent)
  7. /* sends a Webmention to target.
  8. Returns: < 0, error; 0, no Webmention endpoint; > 0, Webmention sent */
  9. {
  10. int status = 0;
  11. xs *endpoint = NULL;
  12. xs *ua = xs_fmt("%s (Webmention)", user_agent ? user_agent : "xs_webmention");
  13. xs *headers = xs_dict_new();
  14. headers = xs_dict_set(headers, "accept", "text/html");
  15. headers = xs_dict_set(headers, "user-agent", ua);
  16. xs *h_req = NULL;
  17. int p_size = 0;
  18. /* try first a HEAD, to see if there is a Webmention Link header */
  19. h_req = xs_http_request("HEAD", target, headers, NULL, 0, &status, NULL, &p_size, 0);
  20. /* return immediate failures */
  21. if (status < 200 || status > 299)
  22. return -1;
  23. const char *link = xs_dict_get(h_req, "link");
  24. if (xs_is_string(link) && xs_regex_match(link, "rel *= *(\"|')?webmention")) {
  25. /* endpoint is between < and > */
  26. xs *r = xs_regex_select_n(link, "<[^>]+>", 1);
  27. if (xs_list_len(r) == 1) {
  28. endpoint = xs_dup(xs_list_get(r, 0));
  29. endpoint = xs_strip_chars_i(endpoint, "<>");
  30. }
  31. }
  32. if (endpoint == NULL) {
  33. /* no Link header; get the content */
  34. xs *g_req = NULL;
  35. xs *payload = NULL;
  36. g_req = xs_http_request("GET", target, headers, NULL, 0, &status, &payload, &p_size, 0);
  37. if (status < 200 || status > 299)
  38. return -1;
  39. const char *ctype = xs_dict_get(g_req, "content-type");
  40. /* not HTML? no point in looking inside */
  41. if (!xs_is_string(ctype) || xs_str_in(ctype, "text/html") == -1)
  42. return -2;
  43. if (!xs_is_string(payload))
  44. return -3;
  45. xs *links = xs_regex_select(payload, "<(a +|link +)[^>]+>");
  46. const char *link;
  47. xs_list_foreach(links, link) {
  48. if (xs_regex_match(link, "rel *= *(\"|')?webmention")) {
  49. /* found; extract the href */
  50. xs *r = xs_regex_select_n(link, "href *= *(\"|')?[^\"]+(\"|')", 1);
  51. if (xs_list_len(r) == 1) {
  52. xs *l = xs_split_n(xs_list_get(r, 0), "=", 1);
  53. if (xs_list_len(l) == 2) {
  54. endpoint = xs_dup(xs_list_get(l, 1));
  55. endpoint = xs_strip_chars_i(endpoint, " \"'");
  56. break;
  57. }
  58. }
  59. }
  60. }
  61. }
  62. /* is it a relative endpoint? */
  63. if (xs_is_string(endpoint)) {
  64. if (!xs_startswith(endpoint, "https://") && !xs_startswith(endpoint, "http://")) {
  65. xs *l = xs_split(target, "/");
  66. if (xs_list_len(l) < 3)
  67. endpoint = xs_free(endpoint);
  68. else {
  69. xs *s = xs_fmt("%s/" "/%s", xs_list_get(l, 0), xs_list_get(l, 2));
  70. endpoint = xs_str_wrap_i(s, endpoint, NULL);
  71. }
  72. }
  73. }
  74. if (xs_is_string(endpoint)) {
  75. /* got it! */
  76. headers = xs_dict_set(headers, "content-type", "application/x-www-form-urlencoded");
  77. xs *body = xs_fmt("source=%s&target=%s", source, target);
  78. xs *rsp = xs_http_request("POST", endpoint, headers, body, strlen(body), &status, NULL, 0, 0);
  79. if (status < 200 || status > 299)
  80. status = -4;
  81. else
  82. status = 1;
  83. }
  84. else
  85. status = 0;
  86. return status;
  87. }
  88. #endif /* XS_IMPLEMENTATION */
  89. #endif /* _XS_WEBMENTION_H */