{"action":"created","comment":{"url":"https://api.github.com/repos/citra-emu/citra/pulls/comments/30457238","id":30457238,"diff_hunk":"@@ -0,0 +1,159 @@\n+// Copyright 2015 Citra Emulator Project\n+// Licensed under GPLv2 or any later version\n+// Refer to the license.txt file included.\n+\n+#include <chrono>\n+#include <curl/curl.h>\n+\n+#include \"common/string_util.h\"\n+\n+#include \"core/hle/service/http/http.h\"\n+#include \"core/hle/service/http/http_c.h\"\n+#include \"core/hle/service/service.h\"\n+\n+namespace Service {\n+namespace HTTP {\n+\n+std::unordered_map<ContextHandle, std::unique_ptr<HttpContext>> context_map;\n+ContextHandle next_handle;\n+\n+static int BufWriter(u8 *data, size_t size, size_t nmemb, std::vector<u8>* out_buffer) {\n+    if (out_buffer == nullptr)\n+        return -1;\n+    out_buffer->insert(out_buffer->end(), data, data + size * nmemb);\n+    return static_cast<int>(size * nmemb);\n+}\n+\n+HttpContext::HttpContext() {\n+    state = REQUEST_STATE_NOT_STARTED;\n+    cancel_request = false;\n+    req_type = REQUEST_TYPE_NONE;\n+    request_hdrs = nullptr;\n+    response_code = 0;\n+    content_length = 0.0;\n+    downloaded_size = 0.0;\n+}\n+\n+HttpContext::~HttpContext() {\n+    curl_slist_free_all(request_hdrs);\n+}\n+\n+static CURLcode SetConnectionType(CURL* connection, RequestType type) {\n+    switch (type) {\n+    case REQUEST_TYPE_GET:\n+        return curl_easy_setopt(connection, CURLOPT_HTTPGET, 1);\n+    case REQUEST_TYPE_POST:\n+    case REQUEST_TYPE_POST_:\n+        return curl_easy_setopt(connection, CURLOPT_POST, 1);\n+    case REQUEST_TYPE_PUT:\n+    case REQUEST_TYPE_PUT_:\n+        return curl_easy_setopt(connection, CURLOPT_UPLOAD, 1);\n+    case REQUEST_TYPE_DELETE:\n+        break; // TODO\n+    case REQUEST_TYPE_HEAD:\n+        return curl_easy_setopt(connection, CURLOPT_NOBODY, 1);\n+    }\n+}\n+\n+void MakeRequest(HttpContext* context) {\n+    CURLM* manager = curl_multi_init();\n+    CURL* connection = curl_easy_init();\n+\n+    CURLcode res;\n+    CURLMcode mres;\n+\n+    {\n+        std::lock_guard<std::mutex> lock(context->mutex);\n+\n+        res = curl_easy_setopt(connection, CURLOPT_URL, context->url.c_str());\n+        res = SetConnectionType(connection, context->req_type);\n+        res = curl_easy_setopt(connection, CURLOPT_HTTPHEADER, context->request_hdrs);\n+    }\n+\n+    std::vector<u8> response_hdrs, response_data;\n+    res = curl_easy_setopt(connection, CURLOPT_HEADERFUNCTION, BufWriter);\n+    res = curl_easy_setopt(connection, CURLOPT_WRITEFUNCTION, BufWriter);\n+    res = curl_easy_setopt(connection, CURLOPT_HEADERDATA, &response_hdrs);\n+    res = curl_easy_setopt(connection, CURLOPT_WRITEDATA, &response_data);\n+\n+    curl_multi_add_handle(manager, connection);\n+\n+    int still_running = 0;\n+    int repeats = 0;\n+    curl_multi_perform(manager, &still_running);\n+\n+    do {\n+        // Number of file descriptors. If this is zero, nothing happened to\n+        // the active connection.\n+        int numfds;\n+\n+        // Set the timeout to 1sec.\n+        if (curl_multi_wait(manager, NULL, 0, 1000, &numfds) != CURLM_OK)\n+            break;\n+\n+        if (numfds == 0) {\n+            // We allow one repeat try--after which, we start to sleep the\n+            // thread until the timeout runs out.\n+            if (repeats++ > 1)\n+                std::this_thread::sleep_for(std::chrono::milliseconds(100));\n+        } else {\n+            // While the file descriptor is responding, there's no need to check for repeats.\n+            repeats = 0;\n+        }\n+\n+        {\n+            std::lock_guard<std::mutex> lock(context->mutex);\n+            if (context->cancel_request)\n+                break;\n+        }\n+\n+        curl_multi_perform(manager, &still_running);\n+    } while (still_running != 0);\n+\n+    {\n+        std::lock_guard<std::mutex> lock(context->mutex);\n+\n+        context->response_hdrs = std::move(response_hdrs);\n+        context->response_data = std::move(response_data);\n+        res = curl_easy_getinfo(connection, CURLINFO_RESPONSE_CODE, &context->response_code);\n+        res = curl_easy_getinfo(connection, CURLINFO_SIZE_DOWNLOAD, &context->downloaded_size);\n+        res = curl_easy_getinfo(connection, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &context->content_length);\n+\n+        context->state = REQUEST_STATE_READY;\n+    }\n+\n+    curl_multi_remove_handle(manager, connection);\n+    curl_easy_cleanup(connection);\n+    curl_multi_cleanup(manager);\n+}\n+\n+void AddRequestHeader(const std::string& name, const std::string& value, curl_slist** hdr_list) {\n+    // TODO: header value is empty\n+    std::string header = Common::StringFromFormat(\"%s: %s\", name.c_str(), value.c_str());\n+    *hdr_list = curl_slist_append(*hdr_list, header.c_str());\n+}\n+\n+void Init() {\n+    AddService(new HTTP_C_Interface);\n+\n+    curl_global_init(CURL_GLOBAL_DEFAULT);\n+}\n+\n+void ClearInstance() {\n+    for (auto& pair : context_map) {\n+        pair.second->cancel_request = true;\n+        if (pair.second->req_thread != nullptr)\n+            pair.second->req_thread->join();\n+    }","path":"src/core/hle/service/http/http.cpp","position":147,"original_position":147,"commit_id":"4cd226860dec79c1e03cc09ad0f3410fdd3f269b","original_commit_id":"4cd226860dec79c1e03cc09ad0f3410fdd3f269b","user":{"login":"yuriks","id":341401,"avatar_url":"https://avatars.githubusercontent.com/u/341401?v=3","gravatar_id":"","url":"https://api.github.com/users/yuriks","html_url":"https://github.com/yuriks","followers_url":"https://api.github.com/users/yuriks/followers","following_url":"https://api.github.com/users/yuriks/following{/other_user}","gists_url":"https://api.github.com/users/yuriks/gists{/gist_id}","starred_url":"https://api.github.com/users/yuriks/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yuriks/subscriptions","organizations_url":"https://api.github.com/users/yuriks/orgs","repos_url":"https://api.github.com/users/yuriks/repos","events_url":"https://api.github.com/users/yuriks/events{/privacy}","received_events_url":"https://api.github.com/users/yuriks/received_events","type":"User","site_admin":false},"body":"You should split this loop in two: one that sets the flags for all threads, and then another one that joins on all threads. This should speed up shutdown somewhat if you have several connections.","created_at":"2015-05-16T01:52:46Z","updated_at":"2015-05-16T01:52:46Z","html_url":"https://github.com/citra-emu/citra/pull/738#discussion_r30457238","pull_request_url":"https://api.github.com/repos/citra-emu/citra/pulls/738","_links":{"self":{"href":"https://api.github.com/repos/citra-emu/citra/pulls/comments/30457238"},"html":{"href":"https://github.com/citra-emu/citra/pull/738#discussion_r30457238"},"pull_request":{"href":"https://api.github.com/repos/citra-emu/citra/pulls/738"}}},"pull_request":{"url":"https://api.github.com/repos/citra-emu/citra/pulls/738","id":35077507,"html_url":"https://github.com/citra-emu/citra/pull/738","diff_url":"https://github.com/citra-emu/citra/pull/738.diff","patch_url":"https://github.com/citra-emu/citra/pull/738.patch","issue_url":"https://api.github.com/repos/citra-emu/citra/issues/738","number":738,"state":"open","locked":false,"title":"Implementation of http:C service.","user":{"login":"archshift","id":5377832,"avatar_url":"https://avatars.githubusercontent.com/u/5377832?v=3","gravatar_id":"","url":"https://api.github.com/users/archshift","html_url":"https://github.com/archshift","followers_url":"https://api.github.com/users/archshift/followers","following_url":"https://api.github.com/users/archshift/following{/other_user}","gists_url":"https://api.github.com/users/archshift/gists{/gist_id}","starred_url":"https://api.github.com/users/archshift/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/archshift/subscriptions","organizations_url":"https://api.github.com/users/archshift/orgs","repos_url":"https://api.github.com/users/archshift/repos","events_url":"https://api.github.com/users/archshift/events{/privacy}","received_events_url":"https://api.github.com/users/archshift/received_events","type":"User","site_admin":false},"body":"Early implementation, but it's enough to run DownloadMii and get Nintendo Zone farther (although I suspect it depends on SSL, which will be a later PR).\r\n\r\n~~Note that the cURL submodule does not currently work and as such the CMake code for it is commented out. I still have to figure out a good way to integrate that.~~\r\n\r\n~~Also, lots of hwtests to be done to verify errors or certain behaviors.~~","created_at":"2015-05-10T05:00:53Z","updated_at":"2015-05-16T01:52:46Z","closed_at":null,"merged_at":null,"merge_commit_sha":"90f4522155083f19d430a48a89f162e8b3d77eea","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/citra-emu/citra/pulls/738/commits","review_comments_url":"https://api.github.com/repos/citra-emu/citra/pulls/738/comments","review_comment_url":"https://api.github.com/repos/citra-emu/citra/pulls/comments{/number}","comments_url":"https://api.github.com/repos/citra-emu/citra/issues/738/comments","statuses_url":"https://api.github.com/repos/citra-emu/citra/statuses/4cd226860dec79c1e03cc09ad0f3410fdd3f269b","head":{"label":"archshift:http","ref":"http","sha":"4cd226860dec79c1e03cc09ad0f3410fdd3f269b","user":{"login":"archshift","id":5377832,"avatar_url":"https://avatars.githubusercontent.com/u/5377832?v=3","gravatar_id":"","url":"https://api.github.com/users/archshift","html_url":"https://github.com/archshift","followers_url":"https://api.github.com/users/archshift/followers","following_url":"https://api.github.com/users/archshift/following{/other_user}","gists_url":"https://api.github.com/users/archshift/gists{/gist_id}","starred_url":"https://api.github.com/users/archshift/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/archshift/subscriptions","organizations_url":"https://api.github.com/users/archshift/orgs","repos_url":"https://api.github.com/users/archshift/repos","events_url":"https://api.github.com/users/archshift/events{/privacy}","received_events_url":"https://api.github.com/users/archshift/received_events","type":"User","site_admin":false},"repo":{"id":19927346,"name":"citra","full_name":"archshift/citra","owner":{"login":"archshift","id":5377832,"avatar_url":"https://avatars.githubusercontent.com/u/5377832?v=3","gravatar_id":"","url":"https://api.github.com/users/archshift","html_url":"https://github.com/archshift","followers_url":"https://api.github.com/users/archshift/followers","following_url":"https://api.github.com/users/archshift/following{/other_user}","gists_url":"https://api.github.com/users/archshift/gists{/gist_id}","starred_url":"https://api.github.com/users/archshift/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/archshift/subscriptions","organizations_url":"https://api.github.com/users/archshift/orgs","repos_url":"https://api.github.com/users/archshift/repos","events_url":"https://api.github.com/users/archshift/events{/privacy}","received_events_url":"https://api.github.com/users/archshift/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/archshift/citra","description":"Nintendo 3DS emulator/debugger","fork":true,"url":"https://api.github.com/repos/archshift/citra","forks_url":"https://api.github.com/repos/archshift/citra/forks","keys_url":"https://api.github.com/repos/archshift/citra/keys{/key_id}","collaborators_url":"https://api.github.com/repos/archshift/citra/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/archshift/citra/teams","hooks_url":"https://api.github.com/repos/archshift/citra/hooks","issue_events_url":"https://api.github.com/repos/archshift/citra/issues/events{/number}","events_url":"https://api.github.com/repos/archshift/citra/events","assignees_url":"https://api.github.com/repos/archshift/citra/assignees{/user}","branches_url":"https://api.github.com/repos/archshift/citra/branches{/branch}","tags_url":"https://api.github.com/repos/archshift/citra/tags","blobs_url":"https://api.github.com/repos/archshift/citra/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/archshift/citra/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/archshift/citra/git/refs{/sha}","trees_url":"https://api.github.com/repos/archshift/citra/git/trees{/sha}","statuses_url":"https://api.github.com/repos/archshift/citra/statuses/{sha}","languages_url":"https://api.github.com/repos/archshift/citra/languages","stargazers_url":"https://api.github.com/repos/archshift/citra/stargazers","contributors_url":"https://api.github.com/repos/archshift/citra/contributors","subscribers_url":"https://api.github.com/repos/archshift/citra/subscribers","subscription_url":"https://api.github.com/repos/archshift/citra/subscription","commits_url":"https://api.github.com/repos/archshift/citra/commits{/sha}","git_commits_url":"https://api.github.com/repos/archshift/citra/git/commits{/sha}","comments_url":"https://api.github.com/repos/archshift/citra/comments{/number}","issue_comment_url":"https://api.github.com/repos/archshift/citra/issues/comments{/number}","contents_url":"https://api.github.com/repos/archshift/citra/contents/{+path}","compare_url":"https://api.github.com/repos/archshift/citra/compare/{base}...{head}","merges_url":"https://api.github.com/repos/archshift/citra/merges","archive_url":"https://api.github.com/repos/archshift/citra/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/archshift/citra/downloads","issues_url":"https://api.github.com/repos/archshift/citra/issues{/number}","pulls_url":"https://api.github.com/repos/archshift/citra/pulls{/number}","milestones_url":"https://api.github.com/repos/archshift/citra/milestones{/number}","notifications_url":"https://api.github.com/repos/archshift/citra/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/archshift/citra/labels{/name}","releases_url":"https://api.github.com/repos/archshift/citra/releases{/id}","created_at":"2014-05-19T02:50:51Z","updated_at":"2015-04-11T08:09:31Z","pushed_at":"2015-05-16T00:53:45Z","git_url":"git://github.com/archshift/citra.git","ssh_url":"git@github.com:archshift/citra.git","clone_url":"https://github.com/archshift/citra.git","svn_url":"https://github.com/archshift/citra","homepage":null,"size":9421,"stargazers_count":1,"watchers_count":1,"language":"C++","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":1,"default_branch":"master"}},"base":{"label":"citra-emu:master","ref":"master","sha":"12f6216741fe846c32d1973026634c4c7da86697","user":{"login":"citra-emu","id":7605670,"avatar_url":"https://avatars.githubusercontent.com/u/7605670?v=3","gravatar_id":"","url":"https://api.github.com/users/citra-emu","html_url":"https://github.com/citra-emu","followers_url":"https://api.github.com/users/citra-emu/followers","following_url":"https://api.github.com/users/citra-emu/following{/other_user}","gists_url":"https://api.github.com/users/citra-emu/gists{/gist_id}","starred_url":"https://api.github.com/users/citra-emu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/citra-emu/subscriptions","organizations_url":"https://api.github.com/users/citra-emu/orgs","repos_url":"https://api.github.com/users/citra-emu/repos","events_url":"https://api.github.com/users/citra-emu/events{/privacy}","received_events_url":"https://api.github.com/users/citra-emu/received_events","type":"Organization","site_admin":false},"repo":{"id":19866730,"name":"citra","full_name":"citra-emu/citra","owner":{"login":"citra-emu","id":7605670,"avatar_url":"https://avatars.githubusercontent.com/u/7605670?v=3","gravatar_id":"","url":"https://api.github.com/users/citra-emu","html_url":"https://github.com/citra-emu","followers_url":"https://api.github.com/users/citra-emu/followers","following_url":"https://api.github.com/users/citra-emu/following{/other_user}","gists_url":"https://api.github.com/users/citra-emu/gists{/gist_id}","starred_url":"https://api.github.com/users/citra-emu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/citra-emu/subscriptions","organizations_url":"https://api.github.com/users/citra-emu/orgs","repos_url":"https://api.github.com/users/citra-emu/repos","events_url":"https://api.github.com/users/citra-emu/events{/privacy}","received_events_url":"https://api.github.com/users/citra-emu/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/citra-emu/citra","description":"Nintendo 3DS emulator/debugger","fork":false,"url":"https://api.github.com/repos/citra-emu/citra","forks_url":"https://api.github.com/repos/citra-emu/citra/forks","keys_url":"https://api.github.com/repos/citra-emu/citra/keys{/key_id}","collaborators_url":"https://api.github.com/repos/citra-emu/citra/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/citra-emu/citra/teams","hooks_url":"https://api.github.com/repos/citra-emu/citra/hooks","issue_events_url":"https://api.github.com/repos/citra-emu/citra/issues/events{/number}","events_url":"https://api.github.com/repos/citra-emu/citra/events","assignees_url":"https://api.github.com/repos/citra-emu/citra/assignees{/user}","branches_url":"https://api.github.com/repos/citra-emu/citra/branches{/branch}","tags_url":"https://api.github.com/repos/citra-emu/citra/tags","blobs_url":"https://api.github.com/repos/citra-emu/citra/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/citra-emu/citra/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/citra-emu/citra/git/refs{/sha}","trees_url":"https://api.github.com/repos/citra-emu/citra/git/trees{/sha}","statuses_url":"https://api.github.com/repos/citra-emu/citra/statuses/{sha}","languages_url":"https://api.github.com/repos/citra-emu/citra/languages","stargazers_url":"https://api.github.com/repos/citra-emu/citra/stargazers","contributors_url":"https://api.github.com/repos/citra-emu/citra/contributors","subscribers_url":"https://api.github.com/repos/citra-emu/citra/subscribers","subscription_url":"https://api.github.com/repos/citra-emu/citra/subscription","commits_url":"https://api.github.com/repos/citra-emu/citra/commits{/sha}","git_commits_url":"https://api.github.com/repos/citra-emu/citra/git/commits{/sha}","comments_url":"https://api.github.com/repos/citra-emu/citra/comments{/number}","issue_comment_url":"https://api.github.com/repos/citra-emu/citra/issues/comments{/number}","contents_url":"https://api.github.com/repos/citra-emu/citra/contents/{+path}","compare_url":"https://api.github.com/repos/citra-emu/citra/compare/{base}...{head}","merges_url":"https://api.github.com/repos/citra-emu/citra/merges","archive_url":"https://api.github.com/repos/citra-emu/citra/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/citra-emu/citra/downloads","issues_url":"https://api.github.com/repos/citra-emu/citra/issues{/number}","pulls_url":"https://api.github.com/repos/citra-emu/citra/pulls{/number}","milestones_url":"https://api.github.com/repos/citra-emu/citra/milestones{/number}","notifications_url":"https://api.github.com/repos/citra-emu/citra/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/citra-emu/citra/labels{/name}","releases_url":"https://api.github.com/repos/citra-emu/citra/releases{/id}","created_at":"2014-05-16T18:33:15Z","updated_at":"2015-05-15T09:30:30Z","pushed_at":"2015-05-16T01:00:53Z","git_url":"git://github.com/citra-emu/citra.git","ssh_url":"git@github.com:citra-emu/citra.git","clone_url":"https://github.com/citra-emu/citra.git","svn_url":"https://github.com/citra-emu/citra","homepage":null,"size":23467,"stargazers_count":828,"watchers_count":828,"language":"C++","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":207,"mirror_url":null,"open_issues_count":57,"forks":207,"open_issues":57,"watchers":828,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/citra-emu/citra/pulls/738"},"html":{"href":"https://github.com/citra-emu/citra/pull/738"},"issue":{"href":"https://api.github.com/repos/citra-emu/citra/issues/738"},"comments":{"href":"https://api.github.com/repos/citra-emu/citra/issues/738/comments"},"review_comments":{"href":"https://api.github.com/repos/citra-emu/citra/pulls/738/comments"},"review_comment":{"href":"https://api.github.com/repos/citra-emu/citra/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/citra-emu/citra/pulls/738/commits"},"statuses":{"href":"https://api.github.com/repos/citra-emu/citra/statuses/4cd226860dec79c1e03cc09ad0f3410fdd3f269b"}}}}