Raised This Month: $ Target: $400
 0% 

HTTPRequests: HTTP Wrapper for Socket Extension


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Author Message
clug
Junior Member
Join Date: May 2016
Location: Australia
Old 05-18-2016 , 11:12   HTTPRequests: HTTP Wrapper for Socket Extension
Reply With Quote #1

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 bSuccessint iStatusCodeStringMap tHeaders, const char[] sBodyint iErrorTypeint 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 0tHeadersSnapshot.Length; ++i) {
            
tHeadersSnapshot.GetKey(isKeysizeof(sKey));
            
tHeaders.GetString(sKeysValuesizeof(sValue));
            
PrintToServer("%s => %s"sKeysValue);
        }

        
PrintToServer("response: %s"sBody);
    } else {
        
PrintToServer("failed request with error type %d, error num %d"iErrorTypeiErrorNum);
    }


Last edited by clug; 05-18-2016 at 11:28.
clug is offline
 



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 18:55.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode