AmendHub

Download

jcs

/

detritus

/

request.h

 

(View History)

jcs   request: Rewrite parse_uri, add URI.query, loop request_data_shuffle Latest amendment: 35 on 2024-11-15

1 /*
2 * Copyright (c) 2021-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 #ifndef __REQUEST_H__
18 #define __REQUEST_H__
19
20 #include <stdarg.h>
21 #include "stdint.h"
22
23 #include "detritus.h"
24 /* TODO: don't pass browser for statusf but return specific error codes? */
25 #include "browser.h"
26 #include "tcp.h"
27
28 #define GOPHER_PORT 70
29 #define FINGER_PORT 79
30 #define HTTP_PORT 80
31 #define HTTPS_PORT 443
32 #define GEMINI_PORT 1965
33
34 #define URI_MAX_SCHEME_LEN 20
35 #define URI_MAX_HOSTNAME_LEN 255
36 #define URI_MAX_PORT_LEN 5
37 #define URI_MAX_PATH_LEN 512
38 #define URI_MAX_QUERY_LEN 512
39 #define URI_MAX_STR_LEN (URI_MAX_SCHEME_LEN + 3 + \
40 URI_MAX_HOSTNAME_LEN + \
41 1 + URI_MAX_PORT_LEN + \
42 URI_MAX_PATH_LEN + \
43 1 + URI_MAX_QUERY_LEN)
44
45 struct URI {
46 char *scheme;
47 char *hostname;
48 char *path;
49 char *query;
50 char *str;
51 unsigned short port;
52 };
53
54 struct request {
55 unsigned char buf[(4 * 1500) + 2048]; /* 4*MTU + input */
56 unsigned char input[1500];
57 unsigned long input_len;
58
59 char *output;
60 unsigned long output_len;
61 unsigned long output_pos;
62
63 TCPiopb iopb;
64 StreamPtr stream;
65 wdsEntry wds[2];
66 TCPStatusPB status_pb;
67 bool tcp_done_reading;
68
69 char hostname[URI_MAX_HOSTNAME_LEN];
70 unsigned short port;
71 ip_addr ip;
72 char ip_s[12 + 3 + 1];
73
74 uint8_t tls_id;
75 short tls_state;
76 unsigned char tls_flags;
77
78 struct browser *browser;
79 };
80
81 extern uint8_t tls_req_last_id;
82
83 struct tls_init_request {
84 uint8_t flags[2];
85 #define BLUESCSI_TLS_INIT_REQUEST_FLAG_NO_VERIFY (1 << 0)
86 uint8_t unix_time[4];
87 char hostname[256];
88 };
89
90 struct URI * parse_uri(char *uristr);
91 struct URI * build_relative_uri(struct URI *uri, char *relative, size_t len);
92 char * uri_encode(unsigned char *str);
93
94 /*
95 * queuer is called with wrote=false to set buf and len, then data is
96 * written to socket, then queuer is called again with did_write=true and
97 * len set to the actual bytes written
98 */
99 typedef bool (*request_data_queuer)(struct request *request, void *cookie,
100 char **buf, size_t *len, bool did_write);
101
102 /*
103 * consumer is called with did_read=false and len, and must set buf to
104 * something that can hold len bytes, then data is read into buf, then
105 * consumer is called again with did_read=true and len set to the actual
106 * bytes read
107 */
108 typedef bool (*request_data_consumer)(struct request *request, void *cookie,
109 char **buf, size_t *len, bool did_read);
110
111 /*
112 * shuffle handles actual tcp/tls reading and writing, then calls back to
113 * queuer and consumer when necessary
114 */
115 bool request_data_shuffle(struct request *request,
116 request_data_queuer queuer, void *queuer_cookie,
117 request_data_consumer consumer, void *consumer_cookie);
118
119 struct request * request_connect(struct browser *browser, char *hostname,
120 unsigned short port, bool tls, unsigned char tls_flags);
121 void request_xfree(struct request **requestp);
122
123 /* TLS functions */
124 short scsi_find_tls(void);
125 void scsi_cleanup(void);
126 bool scsi_can_do_tls(void);
127 uint8_t scsi_tls_init(struct tls_init_request *req);
128 bool scsi_tls_close(uint8_t tls_id);
129 short scsi_tls_status(uint8_t tls_id, short *cipherspace,
130 short *plainspace, short *error);
131 size_t scsi_tls_read(uint8_t tls_id, unsigned char **buf, size_t max_size,
132 bool cipher);
133 size_t scsi_tls_write(uint8_t tls_id, unsigned char *buf, size_t buf_size,
134 bool cipher);
135
136 #endif