-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created game suggestion form, 'add game' button, and working POST-bas…
…ed backend
- Loading branch information
Showing
11 changed files
with
373 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#include "curl_post.hpp" | ||
#include <curl/curl.h> | ||
#include <assert.h> | ||
#include <stdexcept> | ||
|
||
using namespace cURL; | ||
|
||
/* Dummy writer that discards returned data from the server. May redo | ||
this later to match spread/tasks/curl.cpp if we want to use | ||
returned data at some point. | ||
*/ | ||
static size_t streamWrite(void *buffer, size_t size, size_t num, void *strm) | ||
{ | ||
return 0; | ||
} | ||
|
||
void PostRequest::upload(const std::string &url, const std::string &useragent, bool disableExpect100) | ||
{ | ||
CURL *curl = curl_easy_init(); | ||
assert(curl); | ||
|
||
curl_httppost *formpost=NULL; | ||
curl_httppost *lastptr=NULL; | ||
curl_slist *headerlist=NULL; | ||
static const char buf[] = "Expect:"; | ||
|
||
for(StrMap::const_iterator it=fields.begin(); it != fields.end(); it++) | ||
{ | ||
curl_formadd(&formpost, &lastptr, | ||
CURLFORM_PTRNAME, it->first.c_str(), | ||
CURLFORM_PTRCONTENTS, it->second.c_str(), | ||
CURLFORM_END); | ||
} | ||
|
||
for(StrMap::const_iterator it=files.begin(); it != files.end(); it++) | ||
{ | ||
curl_formadd(&formpost, &lastptr, | ||
CURLFORM_PTRNAME, it->first.c_str(), | ||
CURLFORM_FILE, it->second.c_str(), | ||
CURLFORM_END); | ||
} | ||
|
||
for(BufMap::const_iterator it=buffers.begin(); it != buffers.end(); it++) | ||
{ | ||
curl_formadd(&formpost, &lastptr, | ||
CURLFORM_PTRNAME, it->first.c_str(), | ||
CURLFORM_BUFFER, it->second.filename.c_str(), | ||
CURLFORM_BUFFERPTR, it->second.ptr, | ||
CURLFORM_BUFFERLENGTH, it->second.size, | ||
CURLFORM_END); | ||
} | ||
|
||
if(disableExpect100) | ||
{ | ||
headerlist = curl_slist_append(headerlist, buf); | ||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist); | ||
} | ||
|
||
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); | ||
|
||
// URL | ||
curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); | ||
|
||
// Set write function so CURL doesn't dump return data to stdout | ||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, streamWrite); | ||
|
||
// For https. Ignore security and just trust the server blindly. | ||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); | ||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0); | ||
|
||
// This is required for multithreading | ||
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); | ||
|
||
// We need to be able to follow redirects | ||
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); | ||
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 30); | ||
|
||
// Don't silently accept failed requests | ||
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1); | ||
|
||
// Pass along referer information whenever we're following a | ||
// redirect. | ||
curl_easy_setopt(curl, CURLOPT_AUTOREFERER, 1); | ||
|
||
// Set user agent string | ||
curl_easy_setopt(curl, CURLOPT_USERAGENT, useragent.c_str()); | ||
|
||
CURLcode res = curl_easy_perform(curl); | ||
|
||
// Cleanup | ||
curl_easy_cleanup(curl); | ||
curl_formfree(formpost); | ||
if(headerlist) | ||
curl_slist_free_all(headerlist); | ||
|
||
if(res != CURLE_OK && res != CURLE_ABORTED_BY_CALLBACK) | ||
{ | ||
std::string msg = curl_easy_strerror(res); | ||
throw std::runtime_error("Error fetching " + url + ":\n" + msg); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#ifndef __CURL_POST_HPP_ | ||
#define __CURL_POST_HPP_ | ||
|
||
#include <string> | ||
#include <map> | ||
|
||
/* Send POST information to an online web form using libCURL. | ||
*/ | ||
namespace cURL | ||
{ | ||
struct PostRequest | ||
{ | ||
struct FileBuf | ||
{ | ||
std::string filename; | ||
void *ptr; // NOTE: Data must be kept in memory until upload is | ||
// finished. | ||
long size; | ||
}; | ||
|
||
typedef std::map<std::string, std::string> StrMap; | ||
typedef std::map<std::string, FileBuf> BufMap; | ||
|
||
StrMap fields; // Form fields listed in fieldname-value format | ||
StrMap files; // Files to upload, in fieldname-filename format | ||
BufMap buffers; // Alternative way to specify files using memory | ||
// buffers. | ||
|
||
/* Perform POST request. Fill out the above members before calling | ||
this function. All referenced data must be kept available until | ||
the function returns. | ||
The disableExpect100 value, if set, disables the "Expect: | ||
100-continue" header (by setting a blank "Expect:" header | ||
instead.) Disabling it is apparently necessary on some servers | ||
that do not implement HTTP 1.1 correctly. | ||
*/ | ||
void upload(const std::string &url, const std::string &useragent, | ||
bool disableExpect100=true); | ||
}; | ||
} | ||
|
||
#endif |
Oops, something went wrong.