summaryrefslogtreecommitdiff
path: root/xs_webmention.h
blob: f9578b42943e0f8f453ba57530995f85262e71db (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* copyright (c) 2025 grunfink et al. / MIT license */

#ifndef _XS_WEBMENTION_H

#define _XS_WEBMENTION_H

int xs_webmention_send(const char *source, const char *target, const char *user_agent);
int xs_webmention_hook(const char *source, const char *target, const char *user_agent);


#ifdef XS_IMPLEMENTATION

#include "xs_http.h"

int xs_webmention_send(const char *source, const char *target, const char *user_agent)
/* sends a Webmention to target.
   Returns: < 0, error; 0, no Webmention endpoint; > 0, Webmention sent */
{
    int status = 0;
    xs *endpoint = NULL;

    xs *ua = xs_fmt("%s (Webmention)", user_agent ? user_agent : "xs_webmention");
    xs *headers = xs_dict_new();
    headers = xs_dict_set(headers, "accept", "text/html");
    headers = xs_dict_set(headers, "user-agent", ua);

    xs *h_req = NULL;
    int p_size = 0;

    /* try first a HEAD, to see if there is a Webmention Link header */
    h_req = xs_http_request("HEAD", target, headers, NULL, 0, &status, NULL, &p_size, 0);

    /* return immediate failures */
    if (!xs_http_valid_status(status))
        return -1;

    const char *link = xs_dict_get(h_req, "link");

    if (xs_is_string(link) && xs_regex_match(link, "rel *= *(\"|')?webmention")) {
        /* endpoint is between < and > */
        xs *r = xs_regex_select_n(link, "<[^>]+>", 1);

        if (xs_list_len(r) == 1) {
            endpoint = xs_dup(xs_list_get(r, 0));
            endpoint = xs_strip_chars_i(endpoint, "<>");
        }
    }

    if (endpoint == NULL) {
        /* no Link header; get the content */
        xs *g_req = NULL;
        xs *payload = NULL;

        g_req = xs_http_request("GET", target, headers, NULL, 0, &status, &payload, &p_size, 0);

        if (!xs_http_valid_status(status))
            return -1;

        const char *ctype = xs_dict_get(g_req, "content-type");

        /* not HTML? no point in looking inside */
        if (!xs_is_string(ctype) || xs_str_in(ctype, "text/html") == -1)
            return -2;

        if (!xs_is_string(payload))
            return -3;

        xs *links = xs_regex_select(payload, "<(a +|link +)[^>]+>");
        const char *link;

        xs_list_foreach(links, link) {
            if (xs_regex_match(link, "rel *= *(\"|')?webmention")) {
                /* found; extract the href */
                xs *r = xs_regex_select_n(link, "href *= *(\"|')?[^\"]+(\"|')", 1);

                if (xs_list_len(r) == 1) {
                    xs *l = xs_split_n(xs_list_get(r, 0), "=", 1);

                    if (xs_list_len(l) == 2) {
                        endpoint = xs_dup(xs_list_get(l, 1));
                        endpoint = xs_strip_chars_i(endpoint, " \"'");

                        break;
                    }
                }
            }
        }
    }

    /* is it a relative endpoint? */
    if (xs_is_string(endpoint)) {
        if (!xs_startswith(endpoint, "https://") && !xs_startswith(endpoint, "http://")) {
            xs *l = xs_split(target, "/");

            if (xs_list_len(l) < 3)
                endpoint = xs_free(endpoint);
            else {
                xs *s = xs_fmt("%s/" "/%s", xs_list_get(l, 0), xs_list_get(l, 2));
                endpoint = xs_str_wrap_i(s, endpoint, NULL);
            }
        }
    }

    if (xs_is_string(endpoint)) {
        /* got it! */
        headers = xs_dict_set(headers, "content-type", "application/x-www-form-urlencoded");

        xs *body = xs_fmt("source=%s&target=%s", source, target);

        xs *rsp = xs_http_request("POST", endpoint, headers, body, strlen(body), &status, NULL, 0, 0);

        if (!xs_http_valid_status(status))
            status = -4;
        else
            status = 1;
    }
    else
        status = 0;

    return status;
}


int xs_webmention_hook(const char *source, const char *target, const char *user_agent)
/* a Webmention has been received for a target that is ours; check if the source
   really contains a link to our target */
{
    int status = 0;

    xs *ua = xs_fmt("%s (Webmention)", user_agent ? user_agent : "xs_webmention");
    xs *headers = xs_dict_new();
    headers = xs_dict_set(headers, "accept", "text/html");
    headers = xs_dict_set(headers, "user-agent", ua);

    xs *g_req = NULL;
    xs *payload = NULL;
    int p_size = 0;

    g_req = xs_http_request("GET", source, headers, NULL, 0, &status, &payload, &p_size, 0);

    if (!xs_http_valid_status(status))
        return -1;

    if (!xs_is_string(payload))
        return -2;

    /* note: a "rogue" webmention can include a link to our target in commented-out HTML code */

    xs *links = xs_regex_select(payload, "<(a +|link +)[^>]+>");
    const char *link;

    status = 0;
    xs_list_foreach(links, link) {
        /* if the link contains our target, it's valid */
        if (xs_str_in(link, target) != -1) {
            status = 1;
            break;
        }
    }

    return status;
}


#endif /* XS_IMPLEMENTATION */

#endif /* _XS_WEBMENTION_H */