AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   HTTPRequests: HTTP Wrapper for Socket Extension (https://forums.alliedmods.net/showthread.php?t=282824)

clug 05-18-2016 11:12

HTTPRequests: HTTP Wrapper for Socket Extension
 
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);
    }



Deathknife 05-18-2016 11:49

Re: HTTPRequests: HTTP Wrapper for Socket Extension
 
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?

KyleS 05-18-2016 19:24

Re: HTTPRequests: HTTP Wrapper for Socket Extension
 
Quote:

Originally Posted by clug (Post 2419953)
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.


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

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