Skip to content

Commit 109066d

Browse files
committed
Cleanup
1 parent 5dd753d commit 109066d

File tree

5 files changed

+49
-34
lines changed

5 files changed

+49
-34
lines changed

extension.h

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ struct CurlContext {
6868
};
6969

7070
struct HTTPRequest {
71-
HTTPRequest(const ke::AString &method, const ke::AString &url, json_t *data = NULL)
72-
: method(method), url(url), data(data), body(NULL), pos(0), size(0)
71+
HTTPRequest(json_t *data = NULL)
7372
{
7473
if (data != NULL)
7574
{
@@ -78,25 +77,19 @@ struct HTTPRequest {
7877
}
7978
}
8079

81-
const ke::AString method;
82-
const ke::AString url;
83-
json_t *data;
84-
85-
char *body;
86-
size_t pos;
87-
size_t size;
80+
char *body = NULL;
81+
size_t pos = 0;
82+
size_t size = 0;
8883
};
8984

9085
struct HTTPResponse {
91-
HTTPResponse() : status(0), data(NULL), hndlData(BAD_HANDLE), body(NULL), size(0) {}
92-
93-
long status;
94-
json_t *data;
95-
Handle_t hndlData;
86+
long status = 0;
87+
json_t *data = NULL;
88+
Handle_t hndlData = BAD_HANDLE;
9689
HTTPHeaderMap headers;
9790

98-
char *body;
99-
size_t size;
91+
char *body = NULL;
92+
size_t size = 0;
10093
};
10194

10295
struct JSONObjectKeys {

httprequestcontext.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ static size_t ReceiveResponseHeader(char *buffer, size_t size, size_t nmemb, voi
7777
HTTPRequestContext::HTTPRequestContext(const ke::AString &method, const ke::AString &url, json_t *data,
7878
struct curl_slist *headers, IChangeableForward *forward, cell_t value,
7979
long connectTimeout, long followLocation, long timeout)
80-
: request(method, url, data), headers(headers), forward(forward), value(value), connectTimeout(connectTimeout), followLocation(followLocation), timeout(timeout)
80+
: request(data), method(method), url(url), headers(headers), forward(forward), value(value),
81+
connectTimeout(connectTimeout), followLocation(followLocation), timeout(timeout)
8182
{}
8283

8384
HTTPRequestContext::~HTTPRequestContext()
@@ -99,22 +100,22 @@ void HTTPRequestContext::InitCurl()
99100
return;
100101
}
101102

102-
if (request.method.compare("POST") == 0)
103+
if (method.compare("POST") == 0)
103104
{
104105
curl_easy_setopt(curl, CURLOPT_POST, 1L);
105106
}
106-
else if (request.method.compare("PUT") == 0)
107+
else if (method.compare("PUT") == 0)
107108
{
108109
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
109110
}
110-
else if (request.method.compare("PATCH") == 0)
111+
else if (method.compare("PATCH") == 0)
111112
{
112-
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, request.method.chars());
113+
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method.chars());
113114
curl_easy_setopt(curl, CURLOPT_POST, 1L);
114115
}
115-
else if (request.method.compare("DELETE") == 0)
116+
else if (method.compare("DELETE") == 0)
116117
{
117-
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, request.method.chars());
118+
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method.chars());
118119
}
119120

120121
curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "");
@@ -131,7 +132,7 @@ void HTTPRequestContext::InitCurl()
131132
curl_easy_setopt(curl, CURLOPT_READDATA, &request);
132133
curl_easy_setopt(curl, CURLOPT_READFUNCTION, &ReadRequestBody);
133134
curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
134-
curl_easy_setopt(curl, CURLOPT_URL, request.url.chars());
135+
curl_easy_setopt(curl, CURLOPT_URL, url.chars());
135136
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
136137
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteResponseBody);
137138
}

httprequestcontext.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class HTTPRequestContext : public IHTTPContext
4040
struct HTTPRequest request;
4141
struct HTTPResponse response;
4242

43+
const ke::AString method;
44+
const ke::AString url;
4345
struct curl_slist *headers;
4446
IChangeableForward *forward;
4547
cell_t value;

pawn/scripting/include/ripext/http.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ methodmap HTTPClient < Handle
9090
{
9191
// Creates a HTTP client.
9292
//
93+
// The HTTPClient must be freed via delete or CloseHandle().
94+
//
9395
// @param baseURL Base URL to the REST API.
9496
public native HTTPClient(const char[] baseURL);
9597

pawn/scripting/include/ripext/json.inc

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
// Encoding flags
2-
#define JSON_MAX_INDENT 0x1F /**< Maximum indentation */
3-
#define JSON_INDENT(%1) ((%1) & JSON_MAX_INDENT) /**< Pretty-print the result and indent with n spaces */
4-
#define JSON_COMPACT 0x20 /**< Compact representation */
5-
#define JSON_ENSURE_ASCII 0x40 /**< Escape all Unicode characters outside the ASCII range */
6-
#define JSON_SORT_KEYS 0x80 /**< Sort object keys */
7-
#define JSON_ENCODE_ANY 0x200 /**< Encode any value */
8-
#define JSON_ESCAPE_SLASH 0x400 /**< Escape / with \/ */
9-
#define JSON_REAL_PRECISION(%1) (((%1) & 0x1F) << 11) /**< Output floats with at most n digits of precision */
10-
#define JSON_EMBED 0x10000 /**< Omit opening and closing braces of the top-level object */
2+
enum
3+
{
4+
JSON_MAX_INDENT = 0x1F, /**< Maximum indentation */
5+
JSON_COMPACT = 0x20, /**< Compact representation */
6+
JSON_ENSURE_ASCII = 0x40, /**< Escape all Unicode characters outside the ASCII range */
7+
JSON_SORT_KEYS = 0x80, /**< Sort object keys */
8+
JSON_ENCODE_ANY = 0x200, /**< Encode any value */
9+
JSON_ESCAPE_SLASH = 0x400, /**< Escape / with \/ */
10+
JSON_EMBED = 0x10000 /**< Omit opening and closing braces of the top-level object */
11+
};
12+
13+
// Pretty-print the result and indent with n spaces
14+
stock int JSON_INDENT(int n)
15+
{
16+
return n & JSON_MAX_INDENT;
17+
}
18+
19+
// Output floats with at most n digits of precision
20+
stock int JSON_REAL_PRECISION(int n)
21+
{
22+
return (n & 0x1F) << 11;
23+
}
1124

1225
// Generic type for encoding JSON.
1326
methodmap JSON < Handle
@@ -17,7 +30,7 @@ methodmap JSON < Handle
1730
// @param file File to write to.
1831
// @param flags Encoding flags.
1932
// @return True on success, false on failure.
20-
public native bool ToFile(const char[] file, int flags = JSON_INDENT(4));
33+
public native bool ToFile(const char[] file, int flags = 0);
2134

2235
// Retrieves the JSON string representation of the object.
2336
//
@@ -52,6 +65,8 @@ methodmap JSONObject < JSON
5265

5366
// Retrieves an array or object value from the object.
5467
//
68+
// The JSON must be freed via delete or CloseHandle().
69+
//
5570
// @param key Key string.
5671
// @return Value read.
5772
// @error Invalid key.
@@ -197,6 +212,8 @@ methodmap JSONArray < JSON
197212

198213
// Retrieves an array or object value from the array.
199214
//
215+
// The JSON must be freed via delete or CloseHandle().
216+
//
200217
// @param index Index in the array.
201218
// @return Value read.
202219
// @error Invalid index.

0 commit comments

Comments
 (0)