Hi all,
I was experiencing some performance issues when stacking up lots of requests using the SteamWorks extension so I decided to put together a fairly lightweight HTTP wrapper for the sockets extension. It should make dealing with HTTP requests easier and more elegant for those who aren't confident working with sockets. The wrapper will be particularly useful to those who need to make a high volume of web requests, and thus can't afford to use the SteamWorks extension due to performance/throughput.
Few things I want to make clear:
- Methodmaps have been used, so SM 1.7 will required to compile
- The wrapper was written with the intention to service lightweight web requests (eg: requests to a HTTP REST API). It hasn't been extensively tested with larger responses, but from pretty limited testing it appears to cope reasonably well.
- It might not follow the HTTP/1.1 specs perfectly (I haven't done a lot of research here -- but testing things so far has been fine)
- Only POST and GET requests are supported. I have no plans to expand to support PUT/DELETE requests at this stage.
Thanks to f0oster for helping with some concepts.
Requirements
Download
Example Usage
PHP Code:
public void OnPluginStart()
{
HTTPRequest req = HTTPRequest("POST", "http://example.com/api/", "OnRequestComplete");
req.debug = true; // debug flag enables server console output for use during development
req.headers.SetString("User-Agent", "HTTPRequests for SourceMod");
req.params.SetString("test", "1");
req.SendRequest();
}
public void OnRequestComplete(bool bSuccess, int iStatusCode, StringMap tHeaders, const char[] sBody, int iErrorType, int iErrorNum)
{
if (bSuccess) {
PrintToServer("finished request with status code %d", iStatusCode);
PrintToServer("headers:");
char sKey[128], sValue[512];
StringMapSnapshot tHeadersSnapshot = tHeaders.Snapshot();
for (int i = 0; i < tHeadersSnapshot.Length; ++i) {
tHeadersSnapshot.GetKey(i, sKey, sizeof(sKey));
tHeaders.GetString(sKey, sValue, sizeof(sValue));
PrintToServer("%s => %s", sKey, sValue);
}
PrintToServer("response: %s", sBody);
} else {
PrintToServer("failed request with error type %d, error num %d", iErrorType, iErrorNum);
}
}