Urho3D/Source/Samples/43_HttpRequestDemo/HttpRequestDemo.cpp
Arnis Lielturks 889367738b
HTTPS support - civetweb update (#2415)
* initial update

* CMake updated for civetweb, https working

* unused file cleanup

* CMake updates to civetweb

* minor update, mingw not yet working

* CMakeLists updated

* fixed for unix builds

* cmake updates, console output updated

* docs updates

* HTTP sample now uses http and https links

* civetweb version 1.11

* additional changes

* mingw fix

* openssl fix

* additional ssl configuration

* osx workaround

* openssl is no longer optional

* openssl is optional

* civetweb cmakelists updates, URHO3D_SSL=1 set for travis

* debug messages removed from SSL/TLS initialization

* link libraries with civetweb

* code style fixes

* cmake updates

* DBE_TAG=latest removed
2020-05-03 12:22:31 +03:00

133 lines
4.1 KiB
C++

//
// Copyright (c) 2008-2020 the Urho3D project.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#include <Urho3D/Core/CoreEvents.h>
#include <Urho3D/Core/ProcessUtils.h>
#include <Urho3D/Input/Input.h>
#include <Urho3D/Network/Network.h>
#include <Urho3D/Network/HttpRequest.h>
#include <Urho3D/UI/Font.h>
#include <Urho3D/UI/Text.h>
#include <Urho3D/UI/UI.h>
#include "HttpRequestDemo.h"
#include <Urho3D/DebugNew.h>
URHO3D_DEFINE_APPLICATION_MAIN(HttpRequestDemo)
HttpRequestDemo::HttpRequestDemo(Context* context) :
Sample(context)
{
}
void HttpRequestDemo::Start()
{
// Execute base class startup
Sample::Start();
// Create the user interface
CreateUI();
// Subscribe to basic events such as update
SubscribeToEvents();
// Set the mouse mode to use in the sample
Sample::InitMouseMode(MM_FREE);
}
void HttpRequestDemo::CreateUI()
{
auto* cache = GetSubsystem<ResourceCache>();
// Construct new Text object
text_ = new Text(context_);
// Set font and text color
text_->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
text_->SetColor(Color(1.0f, 1.0f, 0.0f));
// Align Text center-screen
text_->SetHorizontalAlignment(HA_CENTER);
text_->SetVerticalAlignment(VA_CENTER);
// Add Text instance to the UI root element
GetSubsystem<UI>()->GetRoot()->AddChild(text_);
}
void HttpRequestDemo::SubscribeToEvents()
{
// Subscribe HandleUpdate() function for processing HTTP request
SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(HttpRequestDemo, HandleUpdate));
}
void HttpRequestDemo::HandleUpdate(StringHash eventType, VariantMap& eventData)
{
auto* network = GetSubsystem<Network>();
if (httpRequest_.Null())
#ifdef URHO3D_SSL
httpRequest_ = network->MakeHttpRequest("https://api.ipify.org/?format=json");
#else
httpRequest_ = network->MakeHttpRequest("http://httpbin.org/ip");
#endif
else
{
// Initializing HTTP request
if (httpRequest_->GetState() == HTTP_INITIALIZING)
return;
// An error has occurred
else if (httpRequest_->GetState() == HTTP_ERROR)
{
text_->SetText("An error has occurred: " + httpRequest_->GetError());
UnsubscribeFromEvent("Update");
URHO3D_LOGERRORF("HttpRequest error: %s", httpRequest_->GetError().CString());
}
// Get message data
else
{
if (httpRequest_->GetAvailableSize() > 0)
message_ += httpRequest_->ReadLine();
else
{
text_->SetText("Processing...");
SharedPtr<JSONFile> json(new JSONFile(context_));
json->FromString(message_);
#ifdef URHO3D_SSL
JSONValue val = json->GetRoot().Get("ip");
#else
JSONValue val = json->GetRoot().Get("origin");
#endif
if (val.IsNull())
text_->SetText("Invalid JSON response retrieved!");
else
text_->SetText("Your IP is: " + val.GetString());
UnsubscribeFromEvent("Update");
}
}
}
}