From 08c2ed0c07c4d1ad036673b447b57b72cd344b1f Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Sun, 29 May 2022 11:13:05 -0700 Subject: [PATCH] move json detection to separate function --- src/aws.zig | 52 +++++++++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/src/aws.zig b/src/aws.zig index a7872f2..fe7c886 100644 --- a/src/aws.zig +++ b/src/aws.zig @@ -237,29 +237,8 @@ pub fn Request(comptime action: anytype) type { try reportTraffic(options.client.allocator, "Call Failed", aws_request, response, log.err); return error.HttpFailure; } - // EC2 ignores our accept type, but technically query protocol only - // returns XML as well. So, we'll ignore the protocol here and just - // look at the return type - var isJson: bool = undefined; - for (response.headers) |h| { - if (std.ascii.eqlIgnoreCase("Content-Type", h.name)) { - if (std.mem.startsWith(u8, h.value, "application/json")) { - isJson = true; - } else if (std.mem.startsWith(u8, h.value, "application/x-amz-json-1.0")) { - isJson = true; - } else if (std.mem.startsWith(u8, h.value, "application/x-amz-json-1.1")) { - isJson = true; - } else if (std.mem.startsWith(u8, h.value, "text/xml")) { - isJson = false; - } else if (std.mem.startsWith(u8, h.value, "application/xml")) { - isJson = false; - } else { - log.err("Unexpected content type: {s}", .{h.value}); - return error.UnexpectedContentType; - } - break; - } - } + + const isJson = try isJsonResponse(response.headers); if (!isJson) return try xmlReturn(options, response); @@ -478,6 +457,33 @@ pub fn Request(comptime action: anytype) type { }; } +fn isJsonResponse(headers: []awshttp.Header) !bool { + // EC2 ignores our accept type, but technically query protocol only + // returns XML as well. So, we'll ignore the protocol here and just + // look at the return type + var isJson: ?bool = null; + for (headers) |h| { + if (std.ascii.eqlIgnoreCase("Content-Type", h.name)) { + if (std.mem.startsWith(u8, h.value, "application/json")) { + isJson = true; + } else if (std.mem.startsWith(u8, h.value, "application/x-amz-json-1.0")) { + isJson = true; + } else if (std.mem.startsWith(u8, h.value, "application/x-amz-json-1.1")) { + isJson = true; + } else if (std.mem.startsWith(u8, h.value, "text/xml")) { + isJson = false; + } else if (std.mem.startsWith(u8, h.value, "application/xml")) { + isJson = false; + } else { + log.err("Unexpected content type: {s}", .{h.value}); + return error.UnexpectedContentType; + } + break; + } + } + if (isJson == null) return error.ContentTypeNotFound; + return isJson.?; +} /// Get request ID from headers. Caller responsible for freeing memory fn requestIdFromHeaders(request: awshttp.HttpRequest, response: awshttp.HttpResult, options: Options) ![]u8 { var request_id: []u8 = undefined;