Raised This Month: $51 Target: $400
 12% 

HTTPRequests: HTTP Wrapper for Socket Extension


Post New Thread Reply   
 
Thread Tools Display Modes
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
Deathknife
Senior Member
Join Date: Aug 2014
Old 05-18-2016 , 11:49   Re: HTTPRequests: HTTP Wrapper for Socket Extension
Reply With Quote #2

I haven't had any trouble with SteamWorks.
Am I right in assuming that this doesn't support https? Don't sockets have a limit of 4 max per second?
__________________

Last edited by Deathknife; 05-18-2016 at 11:50.
Deathknife is offline
KyleS
SourceMod Plugin Approver
Join Date: Jul 2009
Location: Segmentation Fault.
Old 05-18-2016 , 19:24   Re: HTTPRequests: HTTP Wrapper for Socket Extension
Reply With Quote #3

Quote:
Originally Posted by clug View Post
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.
What? Where's your code? What's the actual issue? I doubt it's the extension.
KyleS is offline
Reply



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 21:53.


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