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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
const z = @import( "std" );
const u = @import( "util.zig" );
const HttpClient = @import( "tls12" );
const ClientResponse = HttpClient.Response;
const ClientRequest = HttpClient.Request;
const ArenaAllocator = z.heap.ArenaAllocator;
const Allocator = z.mem.Allocator;
const print = z.debug.print;
pub const Header = z.http.Header;
pub const OnChunkFunc = *const fn ( []const u8 ) void;
pub fn OnChunkFuncWithData( t: type ) type {
return *const fn ( []const u8, t ) void;
}
pub const OnReqEndFunc = *const fn( *Result ) void;
pub fn OnReqEndFuncWithData( t: type ) type {
return *const fn( *Result, t ) void;
}
const ParamsBase = struct {
url: []const u8,
body: ?[]const u8 = null,
method: z.http.Method = z.http.Method.GET,
headers: ?[]const Header = null,
};
pub fn Params() type {
return u.MergeTypes( ParamsBase, struct { chunk_fn: ?OnChunkFunc = null } );
}
pub fn ParamsAsync() type {
return u.MergeTypes( ParamsBase, struct { end_fn: ?OnReqEndFunc = null, chunk_fn: ?OnChunkFunc = null, mutex: z.Thread.Mutex = .{} } );
}
pub fn ParamsWithData( t: type ) type {
return u.MergeTypes( ParamsBase, struct { chunk_fn: ?OnChunkFuncWithData(t), chunk_data: t = undefined } );
}
pub fn ParamsWithDataAsync( t: type ) type {
return u.MergeTypes( ParamsBase, struct { end_fn: ?OnReqEndFuncWithData(t), chunk_fn: ?OnChunkFuncWithData(t), chunk_data: t = undefined, mutex: z.Thread.Mutex = .{} } );
}
pub const Result = struct {
ok: bool = false,
url: []const u8 = &[_]u8{ 0 },
status: z.http.Status = z.http.Status.teapot,
body: ?[]const u8 = null,
headers: z.ArrayList( z.http.Header ),
alloc: z.mem.Allocator = undefined,
pub fn deinit( self: *const Result ) void {
if( self.body ) |*body|
self.alloc.free( body.* );
self.headers.deinit();
}
};
pub fn send( params: Params(), a: Allocator ) Result {
var ret = Result{
.alloc = a,
.url = params.url,
.headers = z.ArrayList( Header ).init( a )
};
const uri = z.Uri.parse( params.url ) catch return ret;
var client = HttpClient{ .allocator = a };
defer client.deinit();
const header_buf: []u8 = a.alloc( u8, 1024 * 8 ) catch return ret;
defer a.free( header_buf );
var req = client.open( params.method, uri, .{
.server_header_buffer = header_buf
}) catch |e| { z.debug.print( "{any}\n", .{ e } ); return ret; };
defer req.deinit();
if( params.headers ) |hdrs|
req.extra_headers = hdrs;
if( params.body != null and params.method == z.http.Method.POST )
req.transfer_encoding = .{ .content_length = params.body.?.len };
req.send() catch return ret;
if( params.body != null and params.method == z.http.Method.POST ) {
var writer = req.writer();
writer.writeAll( params.body.? ) catch return ret;
}
req.finish() catch return ret;
req.wait() catch return ret;
ret.ok = true;
if( params.chunk_fn != null ) {
receiveChunked( &ret, &req, params, a ) catch { ret.ok = false; };
} else {
receiveSingle( &ret, &req, a ) catch { ret.ok = false; };
}
receiveHeaders( &ret, &req );
return ret;
}
///compared to send, takes an additional data arg that gets passed to the chunk_fn
pub fn sendWithData( t: type, params: ParamsWithData(t), a: Allocator ) Result {
var ret = Result{
.alloc = a,
.url = params.url,
.headers = z.ArrayList( Header ).init( a )
};
const uri = z.Uri.parse( params.url ) catch return ret;
var client = HttpClient{ .allocator = a };
defer client.deinit();
const header_buf: []u8 = a.alloc( u8, 1024 * 8 ) catch return ret;
defer a.free( header_buf );
var req = client.open( params.method, uri, .{
.server_header_buffer = header_buf
}) catch |e| { z.debug.print( "http error: {any}\n", .{ e } ); return ret; };
defer req.deinit();
if( params.headers ) |hdrs|
req.extra_headers = hdrs;
if( params.body != null and params.method == z.http.Method.POST )
req.transfer_encoding = .{ .content_length = params.body.?.len };
req.send() catch return ret;
if( params.body != null and params.method == z.http.Method.POST ) {
var writer = req.writer();
writer.writeAll( params.body.? ) catch return ret;
}
req.finish() catch return ret;
req.wait() catch return ret;
receiveHeaders( &ret, &req );
if( params.chunk_fn != null ) {
receiveChunkedWithData( t, &ret, &req, params, a ) catch { ret.ok = false; };
} else {
receiveSingle( &ret, &req, a ) catch { ret.ok = false; };
}
ret.ok = req.response.status == .ok;
return ret;
}
///UNFINISHED: DOES NOT WORK YET
pub fn dispatchSendWithData( t: type, params: ParamsWithDataAsync(t), _a: ArenaAllocator ) void {
var arena = _a;
const a = arena.allocator();
defer arena.deinit();
var ret = Result{
.alloc = a,
.url = params.url,
.headers = z.ArrayList( Header ).init( a )
};
defer if( params.end_fn ) |f| f( &ret, params.chunk_data );
const uri = z.Uri.parse( params.url ) catch return;
var client = HttpClient{ .allocator = a };
const header_buf: []u8 = a.alloc( u8, 1024 * 8 ) catch return;
var req = client.open( params.method, uri, .{
.server_header_buffer = header_buf,
}) catch |e| { z.debug.print( "http error: {any}\n", .{ e } ); return; };
if( params.headers ) |hdrs|
req.extra_headers = hdrs;
if( params.body != null and params.method == z.http.Method.POST )
req.transfer_encoding = .{ .content_length = params.body.?.len };
req.send() catch return;
if( params.body != null and params.method == z.http.Method.POST ) {
var writer = req.writer();
writer.writeAll( params.body.? ) catch return;
}
req.finish() catch return;
req.wait() catch return;
receiveHeaders( &ret, &req );
if( params.chunk_fn != null ) {
receiveChunkedWithDataAsync( t, &ret, &req, params, a ) catch { ret.ok = false; };
} else {
receiveSingle( &ret, &req, a ) catch { ret.ok = false; };
}
ret.ok = req.response.status == .ok;
}
pub fn sendWithDataAsync( t: type, params: ParamsWithDataAsync(t), _arena: ArenaAllocator ) void {
var arena = _arena;
const a = arena.allocator();
const paramsc = params;
_=z.Thread.spawn( .{ .allocator = a }, dispatchSendWithData, .{ t, paramsc, arena } ) catch |e| {
arena.deinit();
z.debug.print( "error spawning thread: {any}\n", .{ e } ); return;
};
}
fn receiveHeaders( ret: *Result, req: *ClientRequest ) void {
var iter = req.response.iterateHeaders();
while( iter.next() ) |header|
ret.headers.append( header ) catch continue;
}
fn receiveSingle( ret: *Result, res: *ClientRequest, a: Allocator ) !void {
ret.status = res.response.status;
// fuck it we ball
const slice = try res.reader().readAllAlloc( a, 1024 * 1024 );
ret.body = slice;
if( ret.status != .ok )
ret.ok = false;
}
fn receiveChunked( ret: *Result, req: *ClientRequest, q: Params(), a: Allocator ) !void {
ret.status = req.response.status;
var full_str = z.ArrayList( u8 ).init( a );
defer full_str.deinit();
const writer = full_str.writer();
var buf = try a.alloc( u8, 1024 * 16 );
defer a.free( buf );
var bytes_total: usize = 0;
const reader = req.reader();
while( true ) {
const bytes_read = try reader.read( buf );
if( bytes_read == 0 )
break;
bytes_total += bytes_read;
_=try writer.write( buf[0..bytes_read] );
const f = q.chunk_fn;
if( f ) |func| func( buf[0..bytes_read] );
z.Thread.yield() catch {};
}
const slice = try a.alloc( u8, bytes_total );
@memcpy( slice[0..bytes_total], full_str.items[0..bytes_total] );
ret.body = slice;
}
fn receiveChunkedWithData( t: type, ret: *Result, req: *ClientRequest, q: ParamsWithData(t), a: Allocator ) !void {
ret.status = req.response.status;
var full_str = z.ArrayList( u8 ).init( a );
defer full_str.deinit();
const writer = full_str.writer();
var buf = try a.alloc( u8, 1024 * 16 );
defer a.free( buf );
var bytes_total: usize = 0;
const reader = req.reader();
//todo: throw this into another thread
while( true ) {
const bytes_read = try reader.read( buf );
if( bytes_read == 0 )
break;
bytes_total += bytes_read;
_=try writer.write( buf[0..bytes_read] );
const f = q.chunk_fn;
if( f ) |func| func( buf[0..bytes_read], q.chunk_data );
z.Thread.yield() catch {};
}
const slice = try a.alloc( u8, bytes_total );
@memcpy( slice[0..bytes_total], full_str.items[0..bytes_total] );
ret.body = slice;
}
fn receiveChunkedWithDataAsync( t: type, ret: *Result, req: *ClientRequest, q: ParamsWithDataAsync(t), a: Allocator ) !void {
ret.status = req.response.status;
var full_str = z.ArrayList( u8 ).init( a );
defer full_str.deinit();
const writer = full_str.writer();
var buf = try a.alloc( u8, 1024 * 16 );
defer a.free( buf );
var bytes_total: usize = 0;
const reader = req.reader();
//todo: throw this into another thread
while( true ) {
const bytes_read = try reader.read( buf );
if( bytes_read == 0 )
break;
bytes_total += bytes_read;
_=try writer.write( buf[0..bytes_read] );
const f = q.chunk_fn;
if( f ) |func| func( buf[0..bytes_read], q.chunk_data );
z.Thread.yield() catch {};
}
const slice = try a.alloc( u8, bytes_total );
@memcpy( slice[0..bytes_total], full_str.items[0..bytes_total] );
ret.body = slice;
}
|