Everywhere: Make session->header() callable
This commit is contained in:
parent
0960af9aef
commit
71c8db66f1
7 changed files with 62 additions and 29 deletions
|
@ -13,19 +13,19 @@ Bool @slon_activitypub_http_signature_is_valid(SlonHttpSession* session)
|
|||
no_warn scratch_buffer;
|
||||
|
||||
// 1. Check that we have a signature and digest
|
||||
if (!StrLen(@slon_http_request_header(session, "signature")) || !StrLen(@slon_http_request_header(session, "digest"))) {
|
||||
if (!StrLen(session->header("signature")) || !StrLen(session->header("digest"))) {
|
||||
AdamLog("[verify_signature] no signature or digest header present\n");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// 2. Check that digest 1) is SHA-256 and 2) matches content
|
||||
U8* request_digest = @slon_http_request_header(session, "digest");
|
||||
U8* request_digest = session->header("digest");
|
||||
if (!(String.BeginsWith("SHA-256", request_digest) || String.BeginsWith("sha-256", request_digest))) {
|
||||
AdamLog("[verify_signature] digest is not SHA-256\n");
|
||||
return FALSE;
|
||||
}
|
||||
request_digest = StrFind("=", request_digest) + 1;
|
||||
I64 content_length = Str2I64(@slon_http_request_header(session, "content-length"));
|
||||
I64 content_length = Str2I64(session->header("content-length"));
|
||||
if (!content_length) {
|
||||
AdamLog("[verify_signature] content-length is 0\n");
|
||||
return FALSE;
|
||||
|
@ -44,7 +44,7 @@ Bool @slon_activitypub_http_signature_is_valid(SlonHttpSession* session)
|
|||
}
|
||||
|
||||
// Parse values from Signature header
|
||||
U8* signature_header = @slon_http_request_header(session, "signature");
|
||||
U8* signature_header = session->header("signature");
|
||||
I64 signature_fragment_count = 0;
|
||||
U8** signature_fragments = String.Split(signature_header, ',', &signature_fragment_count);
|
||||
|
||||
|
@ -192,7 +192,7 @@ Bool @slon_activitypub_http_signature_is_valid(SlonHttpSession* session)
|
|||
U8** headers_split = String.Split(headers, ' ', &headers_split_count);
|
||||
i = 0;
|
||||
while (i < headers_split_count) {
|
||||
sig_string_alloc_length += StrLen(@slon_http_request_header(session, headers_split[i]));
|
||||
sig_string_alloc_length += StrLen(session->header(headers_split[i]));
|
||||
++i;
|
||||
}
|
||||
sig_string_alloc_length += StrLen(@slon_http_request_verb(session));
|
||||
|
@ -209,7 +209,7 @@ Bool @slon_activitypub_http_signature_is_valid(SlonHttpSession* session)
|
|||
if (!StrCmp("(request-target)", headers_split[i])) {
|
||||
String.Append(sig_string, "(request-target): %s %s", "post", @slon_http_request_path(session));
|
||||
} else {
|
||||
String.Append(sig_string, "%s: %s", headers_split[i], @slon_http_request_header(session, headers_split[i]));
|
||||
String.Append(sig_string, "%s: %s", headers_split[i], session->header(headers_split[i]));
|
||||
}
|
||||
++i;
|
||||
if (i < headers_split_count) {
|
||||
|
|
|
@ -63,6 +63,7 @@ class SlonHttpSession {
|
|||
JsonObject* auth;
|
||||
U8* actor_for_key_id;
|
||||
|
||||
U8* (*header)(U8* key, U8* value = NULL);
|
||||
I64 (*status)(I64 code = NULL);
|
||||
};
|
||||
|
||||
|
@ -148,12 +149,16 @@ JsonObject* @slon_http_request_json(SlonHttpSession* session)
|
|||
|
||||
U0 @slon_http_set_header(SlonHttpSession* session, U8* key, U8* value)
|
||||
{
|
||||
Json.Set(session->response->headers, key, value, JSON_STRING);
|
||||
if (!StrICmp(value, "")) {
|
||||
Json.Unset(session->response->headers, key);
|
||||
} else {
|
||||
Json.Set(session->response->headers, key, value, JSON_STRING);
|
||||
}
|
||||
}
|
||||
|
||||
U0 @slon_http_set_content_type(SlonHttpSession* session, U8* value)
|
||||
{
|
||||
@slon_http_set_header(session, "content-type", value);
|
||||
session->header("content-type", value);
|
||||
}
|
||||
|
||||
U0 @slon_http_send_ap_json(SlonHttpSession* session, U64 json)
|
||||
|
@ -263,3 +268,13 @@ I64 @slon_session_status_wrapper_function(I64 code)
|
|||
}
|
||||
return session->response->status_code;
|
||||
}
|
||||
|
||||
U8* @slon_session_header_wrapper_function(U8* key, U8* value = NULL)
|
||||
{
|
||||
SlonHttpSession* session = SLON_WRAPPER_MAGIC_NUMBER;
|
||||
if (!value) {
|
||||
return @slon_http_request_header(session, key);
|
||||
}
|
||||
@slon_http_set_header(session, key, value);
|
||||
return value;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue