Webternet methodmaps

This commit is contained in:
Erik Minekus 2016-02-19 15:13:09 +01:00 committed by Nicholas Hastings
parent 3e82a46e6b
commit 14031448bd
2 changed files with 146 additions and 2 deletions

View File

@ -477,7 +477,7 @@ static cell_t HTTP_Download(IPluginContext *pCtx, const cell_t *params)
static cell_t HTTP_GetBodySize(IPluginContext *pCtx, const cell_t *params)
{
// 1st param: session handle
// 1st param: downloader handle
Handle_t hndl = static_cast<Handle_t>(params[1]);
HandleError err;
@ -505,7 +505,7 @@ static cell_t HTTP_GetBodySize(IPluginContext *pCtx, const cell_t *params)
static cell_t HTTP_GetBodyContent(IPluginContext *pCtx, const cell_t *params)
{
// 1st param: session handle
// 1st param: downloader handle
Handle_t hndl = static_cast<Handle_t>(params[1]);
HandleError err;
@ -589,6 +589,22 @@ const sp_nativeinfo_t curlext_natives[] =
{"HTTP_AddFileToWebForm", HTTP_AddFileToWebForm},
{"HTTP_GetBodySize", HTTP_GetBodySize},
{"HTTP_GetBodyContent", HTTP_GetBodyContent},
// Methodmap versions
{"HTTPFileDownloader.BodySize.get", HTTP_GetBodySize},
{"HTTPFileDownloader.GetBodyContent", HTTP_GetBodyContent},
{"HTTPMemoryDownloader.BodySize.get", HTTP_GetBodySize},
{"HTTPMemoryDownloader.GetBodyContent", HTTP_GetBodyContent},
{"HTTPSession.SetFailOnHTTPError", HTTP_SetFailOnHTTPError},
{"HTTPSession.GetLastError", HTTP_GetLastError},
{"HTTPSession.Download", HTTP_Download},
{"HTTPSession.PostAndDownload", HTTP_PostAndDownload},
{"HTTPWebForm.AddString", HTTP_AddStringToWebForm},
{"HTTPWebForm.AddFile", HTTP_AddFileToWebForm},
{"HTTPFileDownloader.HTTPFileDownloader", HTTP_CreateFileDownloader},
{"HTTPMemoryDownloader.HTTPMemoryDownloader", HTTP_CreateMemoryDownloader},
{"HTTPSession.HTTPSession", HTTP_CreateSession},
{"HTTPWebForm.HTTPWebForm", HTTP_CreateWebForm},
{NULL, NULL},
};

View File

@ -51,6 +51,134 @@ funcenum HTTP_SessionCallback {
public(Handle:session, bool:succeeded, Handle:downloader, any:data),
};
methodmap HTTPDownloader < Handle {
// Returns the response body content from a download handle.
//
// @param body The buffer to set to copy the body to.
// @param maxlen The maximum string length of the buffer.
// @return True on success, false on fail/error.
//
// @note Use this function to receive the response body as an UTF-8 string.
// Size information can be obtained by using HTTP_GetBodySize()
// Beware that this function fails if the buffer is too small;
// count in one extra byte for the NULL-terminator.
public native bool GetBodyContent(char[] body, int maxlen);
// Returns the size of a finished request response in bytes.
//
// @return The size of the response in bytes, 0 otherwise.
//
// @note Use this function to allocate a string within SP to hold in-memory
// response data. Do not forget to add one extra byte for the termination.
// See HTTP_GetBodyContent() for more information.
property int BodySize {
public native get();
}
}
methodmap HTTPFileDownloader < HTTPDownloader {
// Creates a new file-based download handler. It stores the response as a local file.
//
// @param file The path to the local file the response shall be stored in.
//
// @note Use this function if you want to download (large) binary content
// to disk or e.g. save a Kv-formatted response to a local text file.
public native HTTPFileDownloader(const char[] file);
}
methodmap HTTPMemoryDownloader < HTTPDownloader {
// Creates a new memory-based download handler. It stores the response in memory.
//
// @note Use this function if you want to directly access a (text) response
// from within SP as an UTF-8 encoded string.
// For more information see HTTP_GetBodySize() and HTTP_GetBodyContent()
public native HTTPMemoryDownloader();
}
methodmap HTTPSession < Handle {
// Creates a new ready-to-use HTTP session handle.
//
// @note You can only perform one request (GET, POST, ...) per session handle.
// For multiple sequential asynchronous requests (e.g. in a loop) you have
// to create a new session and free it with CloseHandle()
public native HTTPSession();
// Sets whether an HTTP failure (>= 400) returns false from the request functions.
//
// @param fail True to fail, false otherwise.
// @return True on success, false on fail/error.
//
// @note The default behaviour is to treat HTTP 4xx errors as an acceptable
// condition. The corresponding error page generated by the web server
// is returned. If set to true, request functions report a failure.
public native bool SetFailOnHTTPError(bool fail);
// Returns the last error happened on a session as human readable message.
//
// @param error The target buffer to hold the error message.
// @param maxlen The maximum string length of the buffer.
//
// @return True on success, false on fail/error.
public native bool GetLastError(char[] error, int maxlen);
// Initiates an asynchronous download attempt (GET-request) for a given URL.
//
// @param downloader The download handle this download should use.
// @param url The target URL.
// @param callback The callback function to be executed on request completion.
// @param data Optional custom user data.
// @return True on success, false on fail/error.
//
// @note While the download action is in progress, the session handle is
// invalid and can't be accessed. For multiple parallel requests
// create a new session with HTTP_CreateSession() for every request.
// Session and download handle have to be freed with CloseHandle()
// after the corresponding callback function has been executed.
public native bool Download(HTTPDownloader downloader, const char[] url, HTTP_SessionCallback callback, any data = 0);
// Initiates an asynchronous download attempt (POST-request) for a given URL.
//
// @param downloader The download handle this download should use.
// @param downloader The web form handle this download should use.
// @param url The target URL.
// @param callback The callback function to be executed on request completion.
// @param data Optional custom user data.
// @return True on success, false on fail/error.
//
// @note While the download action is in progress, the session handle is
// invalid and can't be accessed. For multiple parallel requests
// create a new session with HTTP_CreateSession() for every request.
// Session and download handle have to be freed with CloseHandle()
// after the corresponding callback function has been executed.
// The web form handle is not closed as it may be reused.
public native bool PostAndDownload(HTTPDownloader downloader, HTTPWebForm form, const char[] url, HTTP_SessionCallback callback, any data = 0);
}
methodmap HTTPWebForm < Handle {
// Creates a new empty web form handle.
//
// @note This function may be used with HTTP_PostAndDownload() to simulate
// an application/x-www-form-urlencoded form POST request.
// Also see HTTP_AddStringToWebForm() and HTTP_AddFileToWebForm()
public native HTTPWebForm();
// Adds a text key-value-pair (e.g. game=hl2) to an existing web form.
//
// @param name The name section of the pair (e.g. game)
// @param data The value section of the pair (e.g. hl2)
// @return True on success, false on fail/error.
//
// @note The supplied data gets URL-encoded automatically.
public native bool AddString(const char[] name, const char[] data);
// Adds a local file to upload to an existing web form.
//
// @param name The field name.
// @param path The local file path.
// @return True on success, false on fail/error.
public native bool AddFile(const char[] name, const char[] path);
}
/**
* Creates a new ready-to-use HTTP session handle.
*