Linux HTTP implementation
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "dusk.h"
|
||||
#include "network/networkhttprequest.h"
|
||||
#include "thread/thread.h"
|
||||
|
||||
#define NETWORK_HTTP_PENDING_MAX 16
|
||||
#define NETWORK_HTTP_URL_MAX 512
|
||||
#define NETWORK_HTTP_BODY_MAX 8192
|
||||
#define NETWORK_HTTP_RESPONSE_MAX 65536
|
||||
#define NETWORK_HTTP_HEADER_MAX 16
|
||||
#define NETWORK_HTTP_HEADER_KEY_MAX 64
|
||||
#define NETWORK_HTTP_HEADER_VAL_MAX 256
|
||||
#define NETWORK_ERROR_MESSAGE_MAX 256
|
||||
|
||||
typedef struct {
|
||||
bool_t used;
|
||||
volatile bool_t resultReady;
|
||||
bool_t isError;
|
||||
|
||||
char_t url[NETWORK_HTTP_URL_MAX];
|
||||
networkhttprequestmethod_t method;
|
||||
char_t body[NETWORK_HTTP_BODY_MAX];
|
||||
bool_t hasBody;
|
||||
char_t headerKeys[NETWORK_HTTP_HEADER_MAX][NETWORK_HTTP_HEADER_KEY_MAX];
|
||||
char_t headerVals[NETWORK_HTTP_HEADER_MAX][NETWORK_HTTP_HEADER_VAL_MAX];
|
||||
uint32_t headerCount;
|
||||
|
||||
networkhttpcallback_t callback;
|
||||
networkhttperrorcallback_t errorCallback;
|
||||
void *user;
|
||||
|
||||
uint16_t status;
|
||||
char_t responseBody[NETWORK_HTTP_RESPONSE_MAX];
|
||||
char_t errorMessage[NETWORK_ERROR_MESSAGE_MAX];
|
||||
|
||||
thread_t thread;
|
||||
} networkhttppendingitem_t;
|
||||
|
||||
typedef struct {
|
||||
networkhttppendingitem_t requests[NETWORK_HTTP_PENDING_MAX];
|
||||
threadmutex_t resultsMutex;
|
||||
} networklinux_t;
|
||||
|
||||
/**
|
||||
* Initializes the network manager. Must be called before any other network
|
||||
* functions.
|
||||
*
|
||||
* @return Any error that occurs.
|
||||
*/
|
||||
errorret_t networkLinuxInit();
|
||||
|
||||
/**
|
||||
* Updates the network manager, called once per frame to handle completed
|
||||
* HTTP requests.
|
||||
*
|
||||
* @return Any error that occurs.
|
||||
*/
|
||||
errorret_t networkLinuxUpdate();
|
||||
|
||||
/**
|
||||
* Disposes the network manager.
|
||||
*
|
||||
* @return Any error that occurs.
|
||||
*/
|
||||
errorret_t networkLinuxDispose();
|
||||
|
||||
/**
|
||||
* Submits an asynchronous HTTP request. The callback will be invoked on the
|
||||
* main thread when next available.
|
||||
*
|
||||
* @param url URL to request.
|
||||
* @param method Method to use for the request.
|
||||
* @param bodyOrNull If POST or PUT, custom body string, can be NULL.
|
||||
* @param headers Array of key-value headers.
|
||||
* @param headerCount Count of headers, can be anything if headers is NULL.
|
||||
* @param user Callback pointer received to the callback.
|
||||
* @param callback The callback to invoke when the request completes.
|
||||
* @param errorCallback The callback to invoke if the request fails.
|
||||
* Note, this doesn't count Non-200 status codes, just
|
||||
* network errors.
|
||||
*/
|
||||
void networkLinuxHTTPRequest(
|
||||
const char_t *url,
|
||||
const networkhttprequestmethod_t method,
|
||||
const char_t *bodyOrNull,
|
||||
const networkhttpheader_t *headers,
|
||||
const uint32_t headerCount,
|
||||
void *user,
|
||||
networkhttpcallback_t callback,
|
||||
networkhttperrorcallback_t errorCallback
|
||||
);
|
||||
Reference in New Issue
Block a user