AmendHub

Download

jcs

/

detritus

/

finger.c

 

(View History)

jcs   *: Use a common plaintext line printer, add page free callback Latest amendment: 52 on 2024-11-21

1 /*
2 * Copyright (c) 2024 joshua stein <jcs@jcs.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #include <stdarg.h>
18 #include <stdio.h>
19 #include <string.h>
20
21 #include "detritus.h"
22
23 bool finger_accept_uri(struct URI *uri);
24 bool finger_request_init(page_handle pageh);
25 bool finger_process(page_handle pageh);
26
27 struct page_handler finger_handler = {
28 finger_accept_uri,
29 finger_request_init,
30 page_queue_output,
31 page_consume_data,
32 page_request_cleanup,
33 finger_process,
34 NULL,
35 };
36
37 bool
38 finger_accept_uri(struct URI *uri)
39 {
40 return (strcasecmp(uri->scheme, "finger") == 0);
41 }
42
43 bool
44 finger_request_init(page_handle pageh)
45 {
46 struct page *page = *pageh;
47 size_t path_len;
48 char *output;
49 size_t output_len;
50
51 if (page->uri->port == 0)
52 page->uri->port = FINGER_PORT;
53
54 if (page->uri->path[0] == '\0' ||
55 (page->uri->path[0] == '/' && page->uri->path[1] == '\0'))
56 path_len = 0;
57 else
58 path_len = strlen(page->uri->path + 1);
59
60 output_len = path_len + 2;
61 output = xmalloc(output_len + 1);
62 if (output == NULL) {
63 warn("Out of memory");
64 return false;
65 }
66 snprintf(output, output_len + 1, "%s\r\n",
67 path_len ? page->uri->path + 1 : "");
68
69 page->request = request_connect(page->browser, page->uri->hostname,
70 page->uri->port, false, 0);
71 if (page->request == NULL) {
72 xfree(&output);
73 return false;
74 }
75 page->request->output_len = output_len;
76 page->request->output = output;
77
78 return true;
79 }
80
81 bool
82 finger_process(page_handle pageh)
83 {
84 struct page *page = *pageh;
85 long n;
86 bool ret = true;
87
88 if (page->content_pos == page->content_len)
89 return PAGE_CAN_READ_MORE(page);
90
91 if (page->content_pos == 0)
92 browser_commit_to_loading_page(page->browser);
93
94 page->browser->style = STYLE_PRE;
95
96 return page_print_plaintext(pageh);
97 }