aboutsummaryrefslogtreecommitdiff
path: root/src/proto
diff options
context:
space:
mode:
authorMartin Ashby <martin@ashbysoft.com>2023-09-27 20:23:30 +0100
committerMartin Ashby <martin@ashbysoft.com>2023-09-27 20:23:30 +0100
commit08472c27c77d27ea084e3458842540351c5a5c28 (patch)
tree67029d8f6779b107ec8d0cf370981045ccea1e30 /src/proto
parent4c5101d5f3a60c58190809166f1aa1eac2e7875f (diff)
downloadpgz-08472c27c77d27ea084e3458842540351c5a5c28.tar.gz
pgz-08472c27c77d27ea084e3458842540351c5a5c28.tar.bz2
pgz-08472c27c77d27ea084e3458842540351c5a5c28.tar.xz
pgz-08472c27c77d27ea084e3458842540351c5a5c28.zip
Add cleartext password handling.
Fix segfault on error response read. Add test for tcp connection and incorrect password
Diffstat (limited to 'src/proto')
-rw-r--r--src/proto/error_response.zig39
1 files changed, 30 insertions, 9 deletions
diff --git a/src/proto/error_response.zig b/src/proto/error_response.zig
index 2aafe8d..dc75053 100644
--- a/src/proto/error_response.zig
+++ b/src/proto/error_response.zig
@@ -28,12 +28,18 @@ routine: ?[]const u8 = null,
unknown_fields: HMByteString,
pub fn read(allocator: std.mem.Allocator, b: []const u8) !ErrorResponse {
- var res: ErrorResponse = undefined;
- res.unknown_fields = HMByteString.init(allocator);
- res.buf = try allocator.dupe(u8, b);
- errdefer allocator.free(res.buf.?);
+ var res = ErrorResponse{
+ .severity = "",
+ .code = "",
+ .message = "",
+ .unknown_fields = HMByteString.init(allocator),
+ .buf = try allocator.dupe(u8, b),
+ };
+ errdefer res.deinit(allocator);
var it = std.mem.splitScalar(u8, res.buf.?, 0);
- var setSev = false; var setCode = false; var setMsg = false;
+ var setSev = false;
+ var setCode = false;
+ var setMsg = false;
while (it.next()) |next| {
if (next.len < 1) break;
switch (next[0]) {
@@ -97,7 +103,7 @@ pub fn read(allocator: std.mem.Allocator, b: []const u8) !ErrorResponse {
},
else => {
try res.unknown_fields.put(next[0], next[1..]);
- }
+ },
}
}
if (!(setSev and setCode and setMsg)) return ProtocolError.MissingField;
@@ -112,11 +118,10 @@ pub fn write(self: ErrorResponse, allocator: std.mem.Allocator, stream_writer: a
try writer.writeIntBig(u32, 0); // Length placeholder.
try write_field_nt('S', self, "severity", writer);
- if (self.severity_unlocalized) |severity_unlocalized| {
- try write_nt('V', severity_unlocalized, writer);
- }
+ if (self.severity_unlocalized) |severity_unlocalized| try write_nt('V', severity_unlocalized, writer);
try write_field_nt('C', self, "code", writer);
try write_field_nt('M', self, "message", writer);
+ if (self.detail) |detail| try write_nt('D', detail, writer);
// TODO rest of the fields
// replace the length and write it to the actual stream
@@ -137,6 +142,20 @@ pub fn deinit(self: *ErrorResponse, allocator: std.mem.Allocator) void {
if (self.buf != null) allocator.free(self.buf.?);
}
+pub fn format(self: ErrorResponse, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {
+ _ = options;
+ _ = fmt;
+ try writer.writeAll("ErrorResponse severity [");
+ try writer.writeAll(self.severity);
+ try writer.writeAll("] ");
+ try writer.writeAll("code [");
+ try writer.writeAll(self.code);
+ try writer.writeAll("] ");
+ try writer.writeAll("message [");
+ try writer.writeAll(self.message);
+ try writer.writeAll("]");
+}
+
test "round trip" {
const allocator = std.testing.allocator;
var sm = ErrorResponse{
@@ -144,6 +163,7 @@ test "round trip" {
.severity_unlocalized = "foo_unlocal",
.code = "bar",
.message = "baz",
+ .detail = "bang, and that's the end",
.unknown_fields = HMByteString.init(allocator),
};
defer sm.deinit(allocator);
@@ -167,4 +187,5 @@ test "round trip" {
try std.testing.expectEqualStrings("foo_unlocal", sm2.severity_unlocalized.?);
try std.testing.expectEqualStrings("bar", sm2.code);
try std.testing.expectEqualStrings("baz", sm2.message);
+ try std.testing.expectEqualStrings("bang, and that's the end", sm2.detail.?);
}