Skip to content
This repository was archived by the owner on Sep 18, 2023. It is now read-only.

Commit

Permalink
🚨 Zig fmt everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
ducdetronquito committed Aug 25, 2021
1 parent a012f69 commit 9856e4d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 97 deletions.
3 changes: 1 addition & 2 deletions src/client.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ const std = @import("std");
const StreamingResponse = @import("response.zig").StreamingResponse;
const Uri = @import("http").Uri;


pub const Client = struct {
allocator: *Allocator,

pub fn init(allocator: *Allocator) !Client {
try network.init();
return Client { .allocator = allocator };
return Client{ .allocator = allocator };
}

pub fn deinit(_: *Client) void {
Expand Down
83 changes: 27 additions & 56 deletions src/connection.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@ const StreamingResponse = @import("response.zig").StreamingResponse;
const tls = @import("iguanaTLS");
const Uri = @import("http").Uri;


pub const TcpConnection = Connection(TcpSocket);

pub const Protocol = enum {
Http,
Https
};
pub const Protocol = enum { Http, Https };

pub fn Connection(comptime SocketType: type) type {
return struct {
Expand All @@ -42,17 +38,14 @@ pub fn Connection(comptime SocketType: type) type {

if (std.mem.eql(u8, uri.scheme, "https")) {
connection.protocol = .Https;
connection.tls_context = try tls.client_connect(
.{
.reader = connection.socket.reader(),
.writer = connection.socket.writer(),
.cert_verifier = .none,
.temp_allocator = allocator,
.ciphersuites = tls.ciphersuites.all,
.protocols = &[_][]const u8{"http/1.1"},
},
uri.host.name
);
connection.tls_context = try tls.client_connect(.{
.reader = connection.socket.reader(),
.writer = connection.socket.writer(),
.cert_verifier = .none,
.temp_allocator = allocator,
.ciphersuites = tls.ciphersuites.all,
.protocols = &[_][]const u8{"http/1.1"},
}, uri.host.name);
}

return connection;
Expand All @@ -76,14 +69,14 @@ pub fn Connection(comptime SocketType: type) type {
}

pub fn read(self: *Self, buffer: []u8) ReaderError!usize {
return switch(self.protocol) {
return switch (self.protocol) {
.Http => self.socket.read(buffer),
.Https => self.tls_context.read(buffer),
};
}

pub fn write(self: *Self, buffer: []const u8) WriterError!usize {
return switch(self.protocol) {
return switch (self.protocol) {
.Http => self.socket.write(buffer),
.Https => self.tls_context.write(buffer),
};
Expand All @@ -99,7 +92,7 @@ pub fn Connection(comptime SocketType: type) type {
errdefer response.deinit();
var body = try self.readResponseBody();

return Response {
return Response{
.allocator = self.allocator,
.buffer = response.raw_bytes,
.status = response.statusCode,
Expand All @@ -117,7 +110,7 @@ pub fn Connection(comptime SocketType: type) type {

var response = try self.readResponse();

return StreamingResponse(Self) {
return StreamingResponse(Self){
.allocator = self.allocator,
.buffer = response.raw_bytes,
.connection = self,
Expand All @@ -130,13 +123,13 @@ pub fn Connection(comptime SocketType: type) type {
fn sendRequest(self: *Self, _request: Request) !void {
var request_event = try h11.Request.init(_request.method, _request.path, _request.version, _request.headers);

try self.state.send(h11.Event {.Request = request_event });
try self.state.send(h11.Event{ .Request = request_event });

switch(_request.body) {
switch (_request.body) {
.Empty => return,
.ContentLength => |body| {
try self.state.send(.{ .Data = h11.Data{ .bytes = body.content } });
}
},
}
}

Expand All @@ -152,7 +145,7 @@ pub fn Connection(comptime SocketType: type) type {
while (true) {
var buffer: [4096]u8 = undefined;
var event = try self.state.nextEvent(.{ .buffer = &buffer });
switch(event) {
switch (event) {
.Data => |data| try body.appendSlice(data.bytes),
.EndOfMessage => return body.toOwnedSlice(),
else => unreachable,
Expand All @@ -166,7 +159,6 @@ pub fn Connection(comptime SocketType: type) type {
};
}


const ConnectionMock = Connection(SocketMock);
const expect = std.testing.expect;
const expectEqualStrings = std.testing.expectEqualStrings;
Expand All @@ -178,10 +170,7 @@ test "Get" {
var connection = try ConnectionMock.connect(std.testing.allocator, uri);
defer connection.deinit();

try connection.socket.target.has_received(
"HTTP/1.1 200 OK\r\nContent-Length: 14\r\nServer: gunicorn/19.9.0\r\n\r\n"
++ "Gotta Go Fast!"
);
try connection.socket.target.has_received("HTTP/1.1 200 OK\r\nContent-Length: 14\r\nServer: gunicorn/19.9.0\r\n\r\n" ++ "Gotta Go Fast!");

var response = try connection.request(.Get, uri, .{});
defer response.deinit();
Expand All @@ -202,16 +191,13 @@ test "Get with headers" {

var connection = try ConnectionMock.connect(std.testing.allocator, uri);
defer connection.deinit();
try connection.socket.target.has_received(
"HTTP/1.1 200 OK\r\nContent-Length: 14\r\nServer: gunicorn/19.9.0\r\n\r\n"
++ "Gotta Go Fast!"
);
try connection.socket.target.has_received("HTTP/1.1 200 OK\r\nContent-Length: 14\r\nServer: gunicorn/19.9.0\r\n\r\n" ++ "Gotta Go Fast!");

var headers = Headers.init(std.testing.allocator);
defer headers.deinit();
try headers.append("Gotta-go", "Fast!");

var response = try connection.request(.Get, uri, .{ .headers = headers.items()});
var response = try connection.request(.Get, uri, .{ .headers = headers.items() });
defer response.deinit();

try expect(connection.socket.target.has_sent("GET /get HTTP/1.1\r\nHost: httpbin.org\r\nGotta-go: Fast!\r\n\r\n"));
Expand All @@ -222,16 +208,11 @@ test "Get with compile-time headers" {

var connection = try ConnectionMock.connect(std.testing.allocator, uri);
defer connection.deinit();
try connection.socket.target.has_received(
"HTTP/1.1 200 OK\r\nContent-Length: 14\r\nServer: gunicorn/19.9.0\r\n\r\n"
++ "Gotta Go Fast!"
);
try connection.socket.target.has_received("HTTP/1.1 200 OK\r\nContent-Length: 14\r\nServer: gunicorn/19.9.0\r\n\r\n" ++ "Gotta Go Fast!");

var headers = .{
.{"Gotta-go", "Fast!"}
};
var headers = .{.{ "Gotta-go", "Fast!" }};

var response = try connection.request(.Get, uri, .{ .headers = headers});
var response = try connection.request(.Get, uri, .{ .headers = headers });
defer response.deinit();

try expect(connection.socket.target.has_sent("GET /get HTTP/1.1\r\nHost: httpbin.org\r\nGotta-go: Fast!\r\n\r\n"));
Expand All @@ -242,12 +223,9 @@ test "Post binary data" {

var connection = try ConnectionMock.connect(std.testing.allocator, uri);
defer connection.deinit();
try connection.socket.target.has_received(
"HTTP/1.1 200 OK\r\nContent-Length: 14\r\nServer: gunicorn/19.9.0\r\n\r\n"
++ "Gotta Go Fast!"
);
try connection.socket.target.has_received("HTTP/1.1 200 OK\r\nContent-Length: 14\r\nServer: gunicorn/19.9.0\r\n\r\n" ++ "Gotta Go Fast!");

var response = try connection.request(.Post, uri, .{ .content = "Gotta go fast!"});
var response = try connection.request(.Post, uri, .{ .content = "Gotta go fast!" });
defer response.deinit();

try expect(connection.socket.target.has_sent("POST /post HTTP/1.1\r\nHost: httpbin.org\r\nContent-Length: 14\r\n\r\nGotta go fast!"));
Expand Down Expand Up @@ -340,7 +318,7 @@ test "Get a streaming response" {
var result = std.ArrayList(u8).init(std.testing.allocator);
defer result.deinit();

while(true) {
while (true) {
var buffer: [4096]u8 = undefined;
var bytesRead = try response.read(&buffer);
if (bytesRead == 0) {
Expand All @@ -357,14 +335,7 @@ test "Get a chunk encoded response" {
var connection = try ConnectionMock.connect(std.testing.allocator, uri);
defer connection.deinit();

try connection.socket.target.has_received(
"HTTP/1.1 200 OK\r\n"
++ "Transfer-Encoding: chunked\r\n\r\n"
++ "7\r\nMozilla\r\n"
++ "9\r\nDeveloper\r\n"
++ "7\r\nNetwork\r\n"
++ "0\r\n\r\n"
);
try connection.socket.target.has_received("HTTP/1.1 200 OK\r\n" ++ "Transfer-Encoding: chunked\r\n\r\n" ++ "7\r\nMozilla\r\n" ++ "9\r\nDeveloper\r\n" ++ "7\r\nNetwork\r\n" ++ "0\r\n\r\n");

var response = try connection.request(.Get, uri, .{});
defer response.deinit();
Expand Down
43 changes: 17 additions & 26 deletions src/request.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@ const std = @import("std");
const Uri = @import("http").Uri;
const Version = @import("http").Version;


const BodyType = enum {
ContentLength,
Empty,
};

const Body = union(BodyType) {
ContentLength: struct {
length: []const u8,
content: []const u8
},
ContentLength: struct { length: []const u8, content: []const u8 },
Empty: void,
};

Expand All @@ -36,17 +32,17 @@ pub const Request = struct {
}
self.headers.deinit();

switch(self.body) {
switch (self.body) {
.ContentLength => |*body| {
self.allocator.free(body.length);
},
else => {}
else => {},
}
}

pub fn init(allocator: *Allocator, method: Method, uri: Uri, options: anytype) !Request {
var path = if (uri.path.len != 0) uri.path else "/";
var request = Request {
var request = Request{
.allocator = allocator,
.body = Body.Empty,
.headers = Headers.init(allocator),
Expand All @@ -57,14 +53,14 @@ pub const Request = struct {
.version = Version.Http11,
};

switch(request.uri.host) {
.ip => |address|{
switch (request.uri.host) {
.ip => |address| {
request.ip = try std.fmt.allocPrint(allocator, "{}", .{address});
try request.headers.append("Host", request.ip.?);
},
.name => |name| {
try request.headers.append("Host", name);
}
},
}

if (@hasField(@TypeOf(options), "headers")) {
Expand All @@ -79,12 +75,10 @@ pub const Request = struct {
if (@hasField(@TypeOf(options), "content")) {
var content_length = std.fmt.allocPrint(allocator, "{d}", .{options.content.len}) catch unreachable;
try request.headers.append("Content-Length", content_length);
request.body = Body {
.ContentLength = .{
.length = content_length,
.content = options.content,
}
};
request.body = Body{ .ContentLength = .{
.length = content_length,
.content = options.content,
} };
}

return request;
Expand All @@ -94,17 +88,16 @@ pub const Request = struct {
const typeof = @TypeOf(user_headers);
const typeinfo = @typeInfo(typeof);

return switch(typeinfo) {
return switch (typeinfo) {
.Struct => Header.as_slice(user_headers),
.Pointer => user_headers,
else => {
@compileError("Invalid headers type: You must provide either a http.Headers or an anonymous struct literal.");
}
},
};
}
};


const expect = std.testing.expect;
const expectEqualStrings = std.testing.expectEqualStrings;

Expand Down Expand Up @@ -136,7 +129,7 @@ test "Request - With user headers" {
defer headers.deinit();
try headers.append("Gotta-go", "Fast!");

var request = try Request.init(std.testing.allocator, .Get, uri, .{ .headers = headers.items()});
var request = try Request.init(std.testing.allocator, .Get, uri, .{ .headers = headers.items() });
defer request.deinit();

try expectEqualStrings(request.headers.items()[0].name.raw(), "Host");
Expand All @@ -148,10 +141,8 @@ test "Request - With user headers" {
test "Request - With compile time user headers" {
const uri = try Uri.parse("http://ziglang.org/news/", false);

var headers = .{
.{"Gotta-go", "Fast!"}
};
var request = try Request.init(std.testing.allocator, .Get, uri, .{ .headers = headers});
var headers = .{.{ "Gotta-go", "Fast!" }};
var request = try Request.init(std.testing.allocator, .Get, uri, .{ .headers = headers });
defer request.deinit();

try expectEqualStrings(request.headers.items()[0].name.raw(), "Host");
Expand All @@ -172,7 +163,7 @@ test "Request - With IP address" {

test "Request - With content" {
const uri = try Uri.parse("http://ziglang.org/news/", false);
var request = try Request.init(std.testing.allocator, .Get, uri, .{ .content = "Gotta go fast!"});
var request = try Request.init(std.testing.allocator, .Get, uri, .{ .content = "Gotta go fast!" });
defer request.deinit();

try expect(request.body == .ContentLength);
Expand Down
4 changes: 1 addition & 3 deletions src/response.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const Version = @import("http").Version;
const ValueTree = std.json.ValueTree;
const Connection = @import("connection.zig").Connection;


pub const Response = struct {
allocator: *Allocator,
buffer: []const u8,
Expand All @@ -31,7 +30,6 @@ pub const Response = struct {
}
};


pub fn StreamingResponse(comptime ConnectionType: type) type {
return struct {
const Self = @This();
Expand All @@ -50,7 +48,7 @@ pub fn StreamingResponse(comptime ConnectionType: type) type {

pub fn read(self: *Self, buffer: []u8) !usize {
var event = try self.connection.nextEvent(.{ .buffer = buffer });
switch(event) {
switch (event) {
.Data => |data| return data.bytes.len,
.EndOfMessage => return 0,
else => unreachable,
Expand Down
Loading

0 comments on commit 9856e4d

Please sign in to comment.