Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Schore <mike.schore@gmail.com>
  • Loading branch information
goaway committed Sep 18, 2020
1 parent 6f1bd25 commit efb8571
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ extern const envoy_filter_data_status_t kEnvoyFilterDataStatusStopIterationNoBuf
typedef struct {
envoy_filter_data_status_t status;
envoy_data data;
envoy_headers extra_headers;
} envoy_filter_data_status;

/**
Expand All @@ -50,6 +51,8 @@ extern const envoy_filter_trailers_status_t kEnvoyFilterTrailersStatusStopIterat
typedef struct {
envoy_filter_trailers_status_t status;
envoy_headers trailers;
envoy_headers extra_headers;
envoy_data extra_data;
} envoy_filter_trailers_status;

#ifdef __cplusplus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ class PlatformBridgeFilter final : public Http::PassThroughFilter,
const std::string filter_name_;
IterationMode iteration_mode_;
envoy_http_filter platform_filter_;
Http::RequestHeaderMap* pending_request_headers_;
Http::ResponseHeaderMap* pending_response_headers_;
Http::RequestTrailerMap* pending_request_trailers_;
Http::ResponseTrialerMap* pending_response_trailers_;
bool forwarded_request_headers_ = false;
bool forwarded_request_end_stream_ = false;
bool forwarded_response_headers_ = false;
bool forwarded_response_end_stream_ = false;
};

} // namespace PlatformBridge
Expand Down
2 changes: 2 additions & 0 deletions library/common/types/c_types.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,5 @@ envoy_data copy_envoy_data(size_t length, const uint8_t* src_bytes) {
}

const envoy_data envoy_nodata = {0, NULL, envoy_noop_release, NULL};

const envoy_headers envoy_noheaders = {0, NULL};
5 changes: 4 additions & 1 deletion library/common/types/c_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ envoy_data copy_envoy_data(size_t length, const uint8_t* src_bytes);
// For example when sending a headers-only request.
extern const envoy_data envoy_nodata;

/**
// Convenience constant to pass to function calls with no headers.
extern const envoy_headers envoy_noheaders;

/*
* Error struct.
*/
typedef struct {
Expand Down
8 changes: 8 additions & 0 deletions library/objective-c/EnvoyBridgeUtility.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#import "library/common/types/c_types.h"

static inline envoy_data toNativeData(NSData *data) {
if (data == nil) {
return envoy_nodata;
}

uint8_t *native_bytes = (uint8_t *)safe_malloc(sizeof(uint8_t) * data.length);
memcpy(native_bytes, data.bytes, data.length);
envoy_data ret = {data.length, native_bytes, free, native_bytes};
Expand All @@ -18,6 +22,10 @@ static inline envoy_data toManagedNativeString(NSString *s) {
}

static inline envoy_headers toNativeHeaders(EnvoyHeaders *headers) {
if (headers == nil) {
return envoy_noheaders;
}

envoy_header_size_t length = 0;
for (NSString *headerKey in headers) {
length += [headers[headerKey] count];
Expand Down
29 changes: 20 additions & 9 deletions library/objective-c/EnvoyEngineImpl.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ static void ios_on_exit(void *context) {
}

EnvoyHeaders *platformHeaders = to_ios_headers(headers);
// TODO(goaway): consider better solution for compound return
NSArray *result = filter.onResponseHeaders(platformHeaders, end_stream);
return (envoy_filter_headers_status){/*status*/ [result[0] intValue],
/*headers*/ toNativeHeaders(result[1])};
Expand All @@ -55,55 +54,67 @@ static envoy_filter_data_status ios_http_filter_on_request_data(envoy_data data,
EnvoyHTTPFilter *filter = (__bridge EnvoyHTTPFilter *)context;
if (filter.onRequestData == nil) {
return (envoy_filter_data_status){/*status*/ kEnvoyFilterDataStatusContinue,
/*data*/ data};
/*data*/ data,
/*extra_headers*/ envoy_noheaders;
}

NSData *platformData = to_ios_data(data);
NSArray *result = filter.onRequestData(platformData, end_stream);
return (envoy_filter_data_status){/*status*/ [result[0] intValue],
/*data*/ toNativeData(result[1])};
/*data*/ toNativeData(result[1]),
/*extra_headers*/ toNativeHeaders(result[2])};
}

static envoy_filter_data_status ios_http_filter_on_response_data(envoy_data data, bool end_stream,
const void *context) {
EnvoyHTTPFilter *filter = (__bridge EnvoyHTTPFilter *)context;
if (filter.onResponseData == nil) {
return (envoy_filter_data_status){/*status*/ kEnvoyFilterDataStatusContinue,
/*data*/ data};
/*data*/ data,
/*extra_headers*/ envoy_noheaders};
}

NSData *platformData = to_ios_data(data);
NSArray *result = filter.onResponseData(platformData, end_stream);
return (envoy_filter_data_status){/*status*/ [result[0] intValue],
/*data*/ toNativeData(result[1])};
/*data*/ toNativeData(result[1]),
/*extra_headers*/ toNativeHeaders(result[2])};
}

static envoy_filter_trailers_status ios_http_filter_on_request_trailers(envoy_headers trailers,
const void *context) {
EnvoyHTTPFilter *filter = (__bridge EnvoyHTTPFilter *)context;
if (filter.onRequestTrailers == nil) {
return (envoy_filter_trailers_status){/*status*/ kEnvoyFilterTrailersStatusContinue,
/*trailers*/ trailers};
/*trailers*/ trailers,
/*extra_headers*/ envoy_noheaders,
/*extra_trailers*/ envoy_nodata};
}

EnvoyHeaders *platformTrailers = to_ios_headers(trailers);
NSArray *result = filter.onRequestTrailers(platformTrailers);
return (envoy_filter_trailers_status){/*status*/ [result[0] intValue],
/*trailers*/ toNativeHeaders(result[1])};
/*trailers*/ toNativeHeaders(result[1]),
/*extra_headers*/ toNativeHeaders(result[2]),
/*extra_data*/ toNativeData(result[3])};
}

static envoy_filter_trailers_status ios_http_filter_on_response_trailers(envoy_headers trailers,
const void *context) {
EnvoyHTTPFilter *filter = (__bridge EnvoyHTTPFilter *)context;
if (filter.onResponseTrailers == nil) {
return (envoy_filter_trailers_status){/*status*/ kEnvoyFilterTrailersStatusContinue,
/*trailers*/ trailers};
/*trailers*/ trailers,
/*extra_headers*/ envoy_noheaders,
/*extra_data*/ envoy_nodata};
}

EnvoyHeaders *platformTrailers = to_ios_headers(trailers);
NSArray *result = filter.onResponseTrailers(platformTrailers);
return (envoy_filter_trailers_status){/*status*/ [result[0] intValue],
/*trailers*/ toNativeHeaders(result[1])};
/*trailers*/ toNativeHeaders(result[1]),
/*extra_headers*/ toNativeHeaders(result[2]),
/*extra_data*/ toNativeData(result[3])};
}

static void ios_http_filter_release(const void *context) {
Expand Down

0 comments on commit efb8571

Please sign in to comment.