Skip to content

Commit

Permalink
MicroHTTPKit: Logging hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
hmelder committed Mar 6, 2024
1 parent 5bfa7c3 commit 597a845
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Libraries/MicroHTTPKit/MicroHTTPKit/HKHTTPServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@

#import <Foundation/Foundation.h>

#import <MicroHTTPKit/HKHTTPRequest.h>
#import <MicroHTTPKit/HKRouter.h>

NS_ASSUME_NONNULL_BEGIN

typedef void (^HKConnectionLogger)(HKHTTPRequest *req);

// The default logger can be overwritten, by changing this global variable.
extern HKConnectionLogger logger;

@interface HKHTTPServer : NSObject

@property (nonatomic, readonly) NSUInteger port;
Expand Down
7 changes: 6 additions & 1 deletion Libraries/MicroHTTPKit/Source/HKHTTPServer.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

#include <microhttpd.h>

HKConnectionLogger logger = ^(HKHTTPRequest *r) {
NSLog(@"%@ %@ Headers: %@ Query Params: %@", [r method], [r URL], [r headers],
[r queryParameters]);
};

// Private methods for request handling
@interface HKHTTPServer (Private)
- (enum MHD_Result)_sendResponseForRequest:(HKHTTPRequest *)request
Expand Down Expand Up @@ -65,7 +70,6 @@ static enum MHD_Result accessHandler(void *cls, struct MHD_Connection *connectio
const char *upload_data, size_t *upload_data_size,
void **con_cls) {
@autoreleasepool {
NSLog(@"Received request for %s %s with body size %lu", method, url, *upload_data_size);
HKHTTPServer *server;
HKHTTPRequest *request;

Expand Down Expand Up @@ -107,6 +111,7 @@ static enum MHD_Result accessHandler(void *cls, struct MHD_Connection *connectio
// Set the request object as the connection class
// This is a __bridge_retained cast, so we need to release the object later on
*con_cls = (__bridge_retained void *) (request);
logger(request);
} else {
// This is a subsequent call for this request, so we need to retrieve the request
// object from the connection class
Expand Down

0 comments on commit 597a845

Please sign in to comment.