1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
use std::os::raw::{c_char, c_uchar, c_uint, c_void};

#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum PlamoScheme {
    PlamoSchemeHttp,
    PlamoSchemeHttps,
}

#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum PlamoHttpVersion {
    PlamoHttpVersionHttp09,
    PlamoHttpVersionHttp10,
    PlamoHttpVersionHttp11,
    PlamoHttpVersionHttp20,
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct PlamoString {
    _unused: [u8; 0],
}

extern "C" {
    pub fn plamo_string_new(value: *const c_char) -> *mut PlamoString;
    pub fn plamo_string_destroy(plamo_string: *mut PlamoString);
    pub fn plamo_string_get_char(plamo_string: *const PlamoString) -> *const c_char;
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct PlamoStringArray {
    _unused: [u8; 0],
}

extern "C" {
    pub fn plamo_string_array_new() -> *mut PlamoStringArray;
    pub fn plamo_string_array_destroy(plamo_string_array: *mut PlamoStringArray);
    pub fn plamo_string_array_for_each(
        plamo_string_array: *const PlamoStringArray,
        callback: Option<unsafe extern "C" fn(arg1: *const c_char)>,
    );
    pub fn plamo_string_array_get_at(
        plamo_string_array: *const PlamoStringArray,
        index: usize,
    ) -> *const c_char;
    pub fn plamo_string_array_get_first(
        plamo_string_array: *const PlamoStringArray,
    ) -> *const c_char;
    pub fn plamo_string_array_get_last(
        plamo_string_array: *const PlamoStringArray,
    ) -> *const c_char;
    pub fn plamo_string_array_add(
        plamo_string_array: *mut PlamoStringArray,
        value: *const c_char,
    );
    pub fn plamo_string_array_remove_at(
        plamo_string_array: *mut PlamoStringArray,
        index: usize,
    ) -> bool;
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct PlamoByteArray {
    _unused: [u8; 0],
}

extern "C" {
    pub fn plamo_byte_array_new(
        body: *const c_uchar,
        length: usize,
    ) -> *mut PlamoByteArray;
    pub fn plamo_byte_array_destroy(plamo_byte_array: *mut PlamoByteArray);
    pub fn plamo_byte_array_get_body(
        plamo_byte_array: *const PlamoByteArray,
    ) -> *const c_uchar;
    pub fn plamo_byte_array_get_body_size(plamo_byte_array: *const PlamoByteArray) -> usize;
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct PlamoHttpHeader {
    _unused: [u8; 0],
}

extern "C" {
    pub fn plamo_http_header_new() -> *mut PlamoHttpHeader;
    pub fn plamo_http_header_destroy(plamo_http_header: *mut PlamoHttpHeader);
    pub fn plamo_http_header_for_each(
        plamo_http_header: *mut PlamoHttpHeader,
        callback: Option<
            unsafe extern "C" fn(arg1: *const c_char, arg2: *mut PlamoStringArray),
        >,
    );
    pub fn plamo_http_header_get(
        plamo_http_header: *mut PlamoHttpHeader,
        key: *const c_char,
    ) -> *mut PlamoStringArray;
    pub fn plamo_http_header_add(
        plamo_http_header: *mut PlamoHttpHeader,
        key: *const c_char,
        value: *const c_char,
    );
    pub fn plamo_http_header_remove(
        plamo_http_header: *mut PlamoHttpHeader,
        key: *const c_char,
    ) -> bool;
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct PlamoHttpQuery {
    _unused: [u8; 0],
}

extern "C" {
    pub fn plamo_http_query_new() -> *mut PlamoHttpQuery;
    pub fn plamo_http_query_destroy(plamo_http_query: *mut PlamoHttpQuery);
    pub fn plamo_http_query_get(
        plamo_http_query: *mut PlamoHttpQuery,
        key: *const c_char,
    ) -> *mut PlamoStringArray;
    pub fn plamo_http_query_add(
        plamo_http_query: *mut PlamoHttpQuery,
        key: *const c_char,
        value: *const c_char,
    );
    pub fn plamo_http_query_remove(
        plamo_http_query: *mut PlamoHttpQuery,
        key: *const c_char,
    ) -> bool;
}

pub type PlamoDefinedHttpMethod = usize;

pub const PLAMO_HTTP_METHOD_GET: PlamoDefinedHttpMethod = usize::max_value();
pub const PLAMO_HTTP_METHOD_POST: PlamoDefinedHttpMethod = usize::max_value() - 1;
pub const PLAMO_HTTP_METHOD_PUT: PlamoDefinedHttpMethod = usize::max_value() - 2;
pub const PLAMO_HTTP_METHOD_DELETE: PlamoDefinedHttpMethod = usize::max_value() - 3;
pub const PLAMO_HTTP_METHOD_HEAD: PlamoDefinedHttpMethod = usize::max_value() - 4;
pub const PLAMO_HTTP_METHOD_CONNECT: PlamoDefinedHttpMethod = usize::max_value() - 5;
pub const PLAMO_HTTP_METHOD_OPTIONS: PlamoDefinedHttpMethod = usize::max_value() - 6;
pub const PLAMO_HTTP_METHOD_TRACE: PlamoDefinedHttpMethod = usize::max_value() - 7;
pub const PLAMO_HTTP_METHOD_PATCH: PlamoDefinedHttpMethod = usize::max_value() - 8;

#[repr(C)]
#[derive(Copy, Clone)]
pub union PlamoHttpMethod {
    pub defined_http_method: PlamoDefinedHttpMethod,
    pub undefined_http_method: *mut c_char,
}

unsafe impl Send for PlamoHttpMethod {}
unsafe impl Sync for PlamoHttpMethod {}

extern "C" {
    pub fn plamo_http_method_new(method: usize) -> PlamoHttpMethod;
    pub fn plamo_http_method_destroy(plamo_http_method: *mut PlamoHttpMethod);
}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct PlamoRequest {
    pub scheme: PlamoScheme,
    pub version: PlamoHttpVersion,
    pub method: PlamoHttpMethod,
    pub path: *mut PlamoString,
    pub query: *mut PlamoHttpQuery,
    pub header: *mut PlamoHttpHeader,
    pub body: *mut PlamoByteArray,
}

extern "C" {
    pub fn plamo_request_new(
        scheme: PlamoScheme,
        version: PlamoHttpVersion,
        method: PlamoHttpMethod,
        path: *const c_char,
        query: *mut PlamoHttpQuery,
        header: *mut PlamoHttpHeader,
        body: *mut PlamoByteArray,
    ) -> *mut PlamoRequest;
    pub fn plamo_request_destroy(plamo_request: *mut PlamoRequest);
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct PlamoResponse {
    pub status_code: c_uint,
    pub header: *mut PlamoHttpHeader,
    pub body: *mut PlamoByteArray,
}

extern "C" {
    pub fn plamo_response_new() -> *mut PlamoResponse;
    pub fn plamo_response_destroy(plamo_response: *mut PlamoResponse);
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct PlamoMiddleware {
    pub config: *const c_void,
    pub callback: Option<
        unsafe extern "C" fn(
            arg1: *const c_void,
            arg2: *const PlamoRequest,
            arg3: *mut PlamoResponse,
        ),
    >,
}

extern "C" {
    pub fn plamo_middleware_new(
        config: *const c_void,
        callback: Option<
            unsafe extern "C" fn(
                arg1: *const c_void,
                arg2: *const PlamoRequest,
                arg3: *mut PlamoResponse,
            ),
        >,
    ) -> *mut PlamoMiddleware;
    pub fn plamo_middleware_destroy(plamo_middleware: *mut PlamoMiddleware);
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct PlamoApp {
    _unused: [u8; 0],
}

extern "C" {
    pub fn plamo_app_new() -> *mut PlamoApp;
    pub fn plamo_app_destroy(plamo_app: *mut PlamoApp);
    pub fn plamo_app_add_middleware(
        plamo_app: *mut PlamoApp,
        plamo_middleware: *const PlamoMiddleware,
    );
    pub fn plamo_app_execute(
        plamo_app: *const PlamoApp,
        plamo_request: *const PlamoRequest,
    ) -> *mut PlamoResponse;
}