mirror of
https://gitea.sffempire.ru/Kolyah35/minecraft-pe-0.6.1.git
synced 2026-03-19 22:43:32 +00:00
Merge remote-tracking branch 'refs/remotes/origin/main'
This commit is contained in:
@@ -12,6 +12,11 @@ endif()
|
|||||||
|
|
||||||
find_package(Threads REQUIRED)
|
find_package(Threads REQUIRED)
|
||||||
|
|
||||||
|
find_package(OpenSSL)
|
||||||
|
if (OpenSSL_FOUND)
|
||||||
|
message(STATUS "found openssl ${OPENSSL_VERSION}")
|
||||||
|
endif()
|
||||||
|
|
||||||
CPMAddPackage("gh:madler/zlib@1.3.2")
|
CPMAddPackage("gh:madler/zlib@1.3.2")
|
||||||
CPMAddPackage(
|
CPMAddPackage(
|
||||||
NAME "libpng"
|
NAME "libpng"
|
||||||
@@ -282,6 +287,11 @@ target_include_directories(${PROJECT_NAME} PUBLIC
|
|||||||
target_compile_definitions(${PROJECT_NAME} PUBLIC "OPENGL_ES" "NO_EGL" ${PLATFORM})
|
target_compile_definitions(${PROJECT_NAME} PUBLIC "OPENGL_ES" "NO_EGL" ${PLATFORM})
|
||||||
target_link_libraries(${PROJECT_NAME} zlib png_shared alsoft.common OpenAL::OpenAL glfw ${EXTRA_LIBS})
|
target_link_libraries(${PROJECT_NAME} zlib png_shared alsoft.common OpenAL::OpenAL glfw ${EXTRA_LIBS})
|
||||||
|
|
||||||
|
if (OpenSSL_FOUND)
|
||||||
|
target_link_libraries(${PROJECT_NAME} OpenSSL::SSL OpenSSL::Crypto)
|
||||||
|
target_compile_definitions(${PROJECT_NAME} PUBLIC HTTPCLIENT_USE_OPENSSL)
|
||||||
|
endif()
|
||||||
|
|
||||||
if (NOT UNIX)
|
if (NOT UNIX)
|
||||||
add_custom_command(
|
add_custom_command(
|
||||||
TARGET ${PROJECT_NAME}
|
TARGET ${PROJECT_NAME}
|
||||||
|
|||||||
31
README.md
31
README.md
@@ -27,15 +27,36 @@ This project aims to preserve and improve this early version of Minecraft PE.
|
|||||||
# Build
|
# Build
|
||||||
|
|
||||||
## CMake
|
## CMake
|
||||||
|
### Linux
|
||||||
|
1. Install dependiences
|
||||||
|
|
||||||
|
(Debian-like)
|
||||||
|
|
||||||
|
``sudo apt install build-essentials git cmake libgl-dev libwayland-dev xorg-dev libxkbcommon-dev``
|
||||||
|
|
||||||
|
(Arch-like)
|
||||||
|
|
||||||
|
``sudo pacman -S base-devel git cmake libglvnd wayland xorg-server-devel xorgproto libxkbcommon``
|
||||||
|
|
||||||
|
2. Create build folder
|
||||||
|
``mkdir build && cd build``
|
||||||
|
|
||||||
|
3. Generate CMake cache and build the project
|
||||||
```
|
```
|
||||||
mkdir build && cd build
|
|
||||||
cmake .. -B .
|
cmake .. -B .
|
||||||
make -j4
|
cmake --build .
|
||||||
```
|
```
|
||||||
or
|
|
||||||
|
### Windows
|
||||||
|
1. Install [Visual studio Build Tools](https://aka.ms/vs/stable/vs_BuildTools.exe) and [CMake](https://github.com/Kitware/CMake/releases/download/v4.3.0-rc3/cmake-4.3.0-rc3-windows-x86_64.msi)
|
||||||
|
|
||||||
|
2. Create build folder
|
||||||
|
``mkdir build && cd build``
|
||||||
|
|
||||||
|
3. Generate CMake cache and build the project
|
||||||
```
|
```
|
||||||
mkdir build && cd build
|
cmake ..
|
||||||
cmake --build . --config Release -j 10
|
cmake --build .
|
||||||
```
|
```
|
||||||
|
|
||||||
## Visual Studio
|
## Visual Studio
|
||||||
|
|||||||
@@ -73,6 +73,8 @@
|
|||||||
#include "../util/Mth.h"
|
#include "../util/Mth.h"
|
||||||
#include "../network/packet/InteractPacket.h"
|
#include "../network/packet/InteractPacket.h"
|
||||||
#include "../network/packet/RespawnPacket.h"
|
#include "../network/packet/RespawnPacket.h"
|
||||||
|
#include "../network/packet/AdventureSettingsPacket.h"
|
||||||
|
#include "../network/packet/SetSpawnPositionPacket.h"
|
||||||
#include "IConfigListener.h"
|
#include "IConfigListener.h"
|
||||||
#include "../world/entity/MobCategory.h"
|
#include "../world/entity/MobCategory.h"
|
||||||
#include "../world/Difficulty.h"
|
#include "../world/Difficulty.h"
|
||||||
@@ -333,6 +335,7 @@ void Minecraft::leaveGame(bool renameLevel /*=false*/)
|
|||||||
|
|
||||||
_running = false;
|
_running = false;
|
||||||
#ifndef STANDALONE_SERVER
|
#ifndef STANDALONE_SERVER
|
||||||
|
gui.clearMessages();
|
||||||
if (renameLevel) {
|
if (renameLevel) {
|
||||||
setScreen(new RenameMPLevelScreen(LevelStorageSource::TempLevelId));
|
setScreen(new RenameMPLevelScreen(LevelStorageSource::TempLevelId));
|
||||||
}
|
}
|
||||||
@@ -725,10 +728,6 @@ void Minecraft::tickInput() {
|
|||||||
setScreen(new ConsoleScreen());
|
setScreen(new ConsoleScreen());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!screen && key == Keyboard::KEY_O || key == 250) {
|
|
||||||
releaseMouse();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (key == Keyboard::KEY_F3) {
|
if (key == Keyboard::KEY_F3) {
|
||||||
options.toggle(OPTIONS_RENDER_DEBUG);
|
options.toggle(OPTIONS_RENDER_DEBUG);
|
||||||
}
|
}
|
||||||
@@ -742,12 +741,15 @@ void Minecraft::tickInput() {
|
|||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!screen && key == Keyboard::KEY_O || key == 250) {
|
||||||
|
releaseMouse();
|
||||||
|
}
|
||||||
|
|
||||||
if (key == Keyboard::KEY_L) {
|
if (key == Keyboard::KEY_F) {
|
||||||
int dst = options.getIntValue(OPTIONS_VIEW_DISTANCE);
|
int dst = options.getIntValue(OPTIONS_VIEW_DISTANCE);
|
||||||
options.set(OPTIONS_VIEW_DISTANCE, (dst + 1) % 4);
|
options.set(OPTIONS_VIEW_DISTANCE, (dst + 1) % 4);
|
||||||
}
|
}
|
||||||
|
#ifdef CHEATS
|
||||||
if (key == Keyboard::KEY_U) {
|
if (key == Keyboard::KEY_U) {
|
||||||
onGraphicsReset();
|
onGraphicsReset();
|
||||||
player->heal(100);
|
player->heal(100);
|
||||||
@@ -829,6 +831,7 @@ void Minecraft::tickInput() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifndef PLATFORM_DESKTOP
|
#ifndef PLATFORM_DESKTOP
|
||||||
if (key == 82)
|
if (key == 82)
|
||||||
|
|||||||
@@ -97,6 +97,8 @@ public:
|
|||||||
|
|
||||||
Options(Minecraft* minecraft, const std::string& workingDirectory = "")
|
Options(Minecraft* minecraft, const std::string& workingDirectory = "")
|
||||||
: minecraft(minecraft) {
|
: minecraft(minecraft) {
|
||||||
|
// elements werent initialized so i was getting a garbage pointer and a crash
|
||||||
|
m_options.fill(nullptr);
|
||||||
initTable();
|
initTable();
|
||||||
load();
|
load();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -135,10 +135,14 @@ void Gui::render(float a, bool mouseFree, int xMouse, int yMouse) {
|
|||||||
#endif
|
#endif
|
||||||
#if defined(RPI)
|
#if defined(RPI)
|
||||||
renderDebugInfo();
|
renderDebugInfo();
|
||||||
#elif defined(PLATFORM_DESKTOP)
|
#endif
|
||||||
|
|
||||||
|
if (Keyboard::isKeyDown(Keyboard::KEY_TAB)) {
|
||||||
|
renderPlayerList(font, screenWidth, screenHeight);
|
||||||
|
}
|
||||||
|
|
||||||
if (minecraft->options.getBooleanValue(OPTIONS_RENDER_DEBUG))
|
if (minecraft->options.getBooleanValue(OPTIONS_RENDER_DEBUG))
|
||||||
renderDebugInfo();
|
renderDebugInfo();
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
glDisable(GL_BLEND);
|
glDisable(GL_BLEND);
|
||||||
@@ -330,6 +334,11 @@ void Gui::addMessage(const std::string& _string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Gui::clearMessages() {
|
||||||
|
guiMessages.clear();
|
||||||
|
chatScrollOffset = 0;
|
||||||
|
}
|
||||||
|
|
||||||
void Gui::setNowPlaying(const std::string& string) {
|
void Gui::setNowPlaying(const std::string& string) {
|
||||||
overlayMessageString = "Now playing: " + string;
|
overlayMessageString = "Now playing: " + string;
|
||||||
overlayMessageTime = 20 * 3;
|
overlayMessageTime = 20 * 3;
|
||||||
@@ -799,6 +808,83 @@ void Gui::renderDebugInfo() {
|
|||||||
t.endOverrideAndDraw();
|
t.endOverrideAndDraw();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Gui::renderPlayerList(Font* font, int screenWidth, int screenHeight) {
|
||||||
|
// only show when in game, no other screen
|
||||||
|
// if (!minecraft->level) return;
|
||||||
|
|
||||||
|
// only show the overlay while connected to a multiplayer server
|
||||||
|
Level* level = minecraft->level;
|
||||||
|
if (!level) return;
|
||||||
|
if (!level->isClientSide) return;
|
||||||
|
|
||||||
|
std::vector<std::string> playerNames;
|
||||||
|
playerNames.reserve(level->players.size());
|
||||||
|
|
||||||
|
for (Player* player : level->players) {
|
||||||
|
if (!player) continue;
|
||||||
|
playerNames.push_back(player->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
// is this check needed? if there are no players, the box won't render at all since height will be 0,
|
||||||
|
// but maybe we want to skip rendering entirely in that case
|
||||||
|
// if (playerNames.empty())
|
||||||
|
// return;
|
||||||
|
|
||||||
|
std::sort(playerNames.begin(), playerNames.end());
|
||||||
|
|
||||||
|
float maxNameWidth = 0.0f;
|
||||||
|
// find the longest name so we can size the box accordingly
|
||||||
|
for (const std::string& name : playerNames) {
|
||||||
|
float nameWidth = font->width(name);
|
||||||
|
if (nameWidth > maxNameWidth)
|
||||||
|
maxNameWidth = nameWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
// player count title
|
||||||
|
std::string titleText = "Players (" + std::to_string(playerNames.size()) + ")";
|
||||||
|
float titleWidth = font->width(titleText);
|
||||||
|
|
||||||
|
if (titleWidth > maxNameWidth)
|
||||||
|
maxNameWidth = titleWidth;
|
||||||
|
|
||||||
|
const float padding = 4.0f;
|
||||||
|
const float lineHeight = (float)Font::DefaultLineHeight;
|
||||||
|
|
||||||
|
const float boxWidth = maxNameWidth + padding * 2;
|
||||||
|
const float boxHeight = (playerNames.size() + 1) * lineHeight + padding * 2;
|
||||||
|
|
||||||
|
const float boxLeft = (screenWidth - boxWidth) / 2.0f;
|
||||||
|
const float boxTop = 10.0f;
|
||||||
|
const float boxRight = boxLeft + boxWidth;
|
||||||
|
const float boxBottom = boxTop + boxHeight;
|
||||||
|
|
||||||
|
fill(boxLeft, boxTop, boxRight, boxBottom, 0x90000000);
|
||||||
|
|
||||||
|
float titleX = (screenWidth - titleWidth) / 2.0f;
|
||||||
|
float titleY = boxTop + padding;
|
||||||
|
|
||||||
|
// scale the text down slightly
|
||||||
|
// i think the gl scaling is the best for this
|
||||||
|
// oh my god this looks really bad OH GOD
|
||||||
|
//const float textScale = 0.8f;
|
||||||
|
//const float invTextScale = 1.0f / textScale;
|
||||||
|
//glPushMatrix2();
|
||||||
|
//glScalef2(textScale, textScale, 1);
|
||||||
|
|
||||||
|
// draw title
|
||||||
|
//font->draw(titleText, titleX * invTextScale, titleY * invTextScale, 0xFFFFFFFF);
|
||||||
|
font->draw(titleText, titleX, titleY, 0xFFFFFFFF);
|
||||||
|
|
||||||
|
// draw player names
|
||||||
|
// we should add ping icons here eventually, but for now just show names
|
||||||
|
float currentY = boxTop + padding + lineHeight;
|
||||||
|
for (const std::string& name : playerNames) {
|
||||||
|
font->draw(name, (boxLeft + padding), currentY, 0xFFDDDDDD);
|
||||||
|
currentY += lineHeight;
|
||||||
|
}
|
||||||
|
//glPopMatrix2();
|
||||||
|
}
|
||||||
|
|
||||||
void Gui::renderSleepAnimation( const int screenWidth, const int screenHeight ) {
|
void Gui::renderSleepAnimation( const int screenWidth, const int screenHeight ) {
|
||||||
int timer = minecraft->player->getSleepTimer();
|
int timer = minecraft->player->getSleepTimer();
|
||||||
float amount = (float) timer / (float) Player::SLEEP_DURATION;
|
float amount = (float) timer / (float) Player::SLEEP_DURATION;
|
||||||
|
|||||||
@@ -61,10 +61,12 @@ public:
|
|||||||
void renderBubbles();
|
void renderBubbles();
|
||||||
void renderHearts();
|
void renderHearts();
|
||||||
void renderDebugInfo();
|
void renderDebugInfo();
|
||||||
|
void renderPlayerList(Font* font, int screenWidth, int screenHeight);
|
||||||
|
|
||||||
void renderProgressIndicator( const bool isTouchInterface, const int screenWidth, const int screenHeight, float a );
|
void renderProgressIndicator( const bool isTouchInterface, const int screenWidth, const int screenHeight, float a );
|
||||||
|
|
||||||
void addMessage(const std::string& string);
|
void addMessage(const std::string& string);
|
||||||
|
void clearMessages();
|
||||||
void postError(int errCode);
|
void postError(int errCode);
|
||||||
|
|
||||||
void onGraphicsReset();
|
void onGraphicsReset();
|
||||||
|
|||||||
@@ -170,22 +170,31 @@ void TouchscreenInput_TestFps::onConfigChanged(const Config& c) {
|
|||||||
//rebuild();
|
//rebuild();
|
||||||
}
|
}
|
||||||
|
|
||||||
void TouchscreenInput_TestFps::setKey( int key, bool state )
|
void TouchscreenInput_TestFps::setKey(int key, bool state)
|
||||||
{
|
{
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
//LOGI("key: %d, %d\n", key, state);
|
//LOGI("key: %d, %d\n", key, state);
|
||||||
|
|
||||||
int id = -1;
|
int id = -1;
|
||||||
if (key == _options->keyUp.key) id = KEY_UP;
|
// theres no keyUp etc???
|
||||||
if (key == _options->keyDown.key) id = KEY_DOWN;
|
//if (key == _options->keyUp.key) id = KEY_UP;
|
||||||
if (key == _options->keyLeft.key) id = KEY_LEFT;
|
//if (key == _options->keyDown.key) id = KEY_DOWN;
|
||||||
if (key == _options->keyRight.key) id = KEY_RIGHT;
|
//if (key == _options->keyLeft.key) id = KEY_LEFT;
|
||||||
if (key == _options->keyJump.key) id = KEY_JUMP;
|
//if (key == _options->keyRight.key) id = KEY_RIGHT;
|
||||||
if (key == _options->keySneak.key) id = KEY_SNEAK;
|
//if (key == _options->keyJump.key) id = KEY_JUMP;
|
||||||
if (key == _options->keyCraft.key) id = KEY_CRAFT;
|
//if (key == _options->keySneak.key) id = KEY_SNEAK;
|
||||||
if (id >= 0) {
|
//if (key == _options->keyCraft.key) id = KEY_CRAFT;
|
||||||
_keys[id] = state;
|
//if (id >= 0) {
|
||||||
}
|
// _keys[id] = state;
|
||||||
|
//}
|
||||||
|
|
||||||
|
if (key == _options->getIntValue(OPTIONS_KEY_FORWARD)) id = KEY_UP;
|
||||||
|
if (key == _options->getIntValue(OPTIONS_KEY_BACK)) id = KEY_DOWN;
|
||||||
|
if (key == _options->getIntValue(OPTIONS_KEY_LEFT)) id = KEY_LEFT;
|
||||||
|
if (key == _options->getIntValue(OPTIONS_KEY_RIGHT)) id = KEY_RIGHT;
|
||||||
|
if (key == _options->getIntValue(OPTIONS_KEY_JUMP)) id = KEY_JUMP;
|
||||||
|
if (key == _options->getIntValue(OPTIONS_KEY_SNEAK)) id = KEY_SNEAK;
|
||||||
|
//if (key == _options->getIntValue(OPTIONS_KEY_CRAFT)) id = KEY_CRAFT;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -167,16 +167,16 @@ void HumanoidMobRenderer::additionalRendering(Mob* mob, float a) {
|
|||||||
const float depth = 1.0f;
|
const float depth = 1.0f;
|
||||||
|
|
||||||
// Front
|
// Front
|
||||||
t.tex(u0, vTop); t.vertex(-halfW, 0.0f, 0.0f);
|
t.tex(u2, vTop); t.vertex(-halfW, 0.0f, 0.0f);
|
||||||
t.tex(u1, vTop); t.vertex(halfW, 0.0f, 0.0f);
|
t.tex(u3, vTop); t.vertex(halfW, 0.0f, 0.0f);
|
||||||
t.tex(u1, vBottom); t.vertex(halfW, height, 0.0f);
|
t.tex(u3, vBottom); t.vertex(halfW, height, 0.0f);
|
||||||
t.tex(u0, vBottom); t.vertex(-halfW, height, 0.0f);
|
t.tex(u2, vBottom); t.vertex(-halfW, height, 0.0f);
|
||||||
|
|
||||||
// Back
|
// Back
|
||||||
t.tex(u2, vTop); t.vertex(halfW, 0.0f, depth);
|
t.tex(u0, vTop); t.vertex(halfW, 0.0f, depth);
|
||||||
t.tex(u3, vTop); t.vertex(-halfW, 0.0f, depth);
|
t.tex(u1, vTop); t.vertex(-halfW, 0.0f, depth);
|
||||||
t.tex(u3, vBottom); t.vertex(-halfW, height, depth);
|
t.tex(u1, vBottom); t.vertex(-halfW, height, depth);
|
||||||
t.tex(u2, vBottom); t.vertex(halfW, height, depth);
|
t.tex(u0, vBottom); t.vertex(halfW, height, depth);
|
||||||
|
|
||||||
// Left
|
// Left
|
||||||
t.tex(uL0, vTop); t.vertex(-halfW, 0.0f, depth);
|
t.tex(uL0, vTop); t.vertex(-halfW, 0.0f, depth);
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
#if defined(ANDROID) && !defined(PRE_ANDROID23)
|
#if defined(ANDROID) && !defined(PRE_ANDROID23)
|
||||||
#include "../../platform/audio/SoundSystemSL.h"
|
#include "../../platform/audio/SoundSystemSL.h"
|
||||||
#elif defined(__APPLE__) || defined(PLATFORM_DESKTOP)
|
#elif (defined(__APPLE__) || defined(PLATFORM_DESKTOP)) && !defined(NO_SOUND)
|
||||||
#include "../../platform/audio/SoundSystemAL.h"
|
#include "../../platform/audio/SoundSystemAL.h"
|
||||||
#else
|
#else
|
||||||
#include "../../platform/audio/SoundSystem.h"
|
#include "../../platform/audio/SoundSystem.h"
|
||||||
@@ -23,7 +23,7 @@ class SoundEngine
|
|||||||
|
|
||||||
#if defined(ANDROID) && !defined(PRE_ANDROID23) && !defined(RPI)
|
#if defined(ANDROID) && !defined(PRE_ANDROID23) && !defined(RPI)
|
||||||
SoundSystemSL soundSystem;
|
SoundSystemSL soundSystem;
|
||||||
#elif defined(__APPLE__) || defined(PLATFORM_DESKTOP)
|
#elif (defined(__APPLE__) || defined(PLATFORM_DESKTOP)) && !defined(NO_SOUND)
|
||||||
SoundSystemAL soundSystem;
|
SoundSystemAL soundSystem;
|
||||||
#else
|
#else
|
||||||
SoundSystem soundSystem;
|
SoundSystem soundSystem;
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ int transformKey(int glfwkey) {
|
|||||||
|
|
||||||
switch (glfwkey) {
|
switch (glfwkey) {
|
||||||
case GLFW_KEY_ESCAPE: return Keyboard::KEY_ESCAPE;
|
case GLFW_KEY_ESCAPE: return Keyboard::KEY_ESCAPE;
|
||||||
|
case GLFW_KEY_TAB: return Keyboard::KEY_TAB;
|
||||||
case GLFW_KEY_BACKSPACE: return Keyboard::KEY_BACKSPACE;
|
case GLFW_KEY_BACKSPACE: return Keyboard::KEY_BACKSPACE;
|
||||||
case GLFW_KEY_LEFT_SHIFT: return Keyboard::KEY_LSHIFT;
|
case GLFW_KEY_LEFT_SHIFT: return Keyboard::KEY_LSHIFT;
|
||||||
case GLFW_KEY_ENTER: return Keyboard::KEY_RETURN;
|
case GLFW_KEY_ENTER: return Keyboard::KEY_RETURN;
|
||||||
|
|||||||
@@ -23,39 +23,57 @@
|
|||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
// if cmake detected opensll, we define HTTPCLIENT_USE_OPENSSL and include the openssl headers
|
||||||
|
// most linux distros should have openssl right
|
||||||
|
#if defined(HTTPCLIENT_USE_OPENSSL)
|
||||||
|
#include <openssl/err.h>
|
||||||
|
#include <openssl/ssl.h>
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
bool stringStartsWith(const std::string& str, const std::string& prefix) {
|
||||||
bool startsWith(const std::string& s, const std::string& prefix) {
|
return str.size() >= prefix.size() && std::equal(prefix.begin(), prefix.end(), str.begin());
|
||||||
return s.size() >= prefix.size() && std::equal(prefix.begin(), prefix.end(), s.begin());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool parseUrl(const std::string& url, std::string& scheme, std::string& host, int& port, std::string& path);
|
bool parseUrl(const std::string& url, std::string& schemeOut, std::string& hostOut, int& portOut, std::string& pathOut);
|
||||||
|
|
||||||
|
// forward declarations for helper functions used by https implementation
|
||||||
|
bool resolveAndConnect(const std::string& host, int port, int& outSocket);
|
||||||
|
bool extractStatusCode(const std::string& headers, int& outStatus);
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
|
|
||||||
bool downloadHttpsWinHttp(const std::string& url, std::vector<unsigned char>& outBody) {
|
// download an https url using windows winhttp
|
||||||
outBody.clear();
|
// this is only used on windows because the rest of the code is a simple raw tcp http client
|
||||||
|
bool downloadHttpsWinHttp(const std::string& url, std::vector<unsigned char>& outputBody) {
|
||||||
|
// gotta start clear
|
||||||
|
outputBody.clear();
|
||||||
|
|
||||||
std::string scheme, host, path;
|
std::string scheme;
|
||||||
|
std::string host;
|
||||||
|
std::string path;
|
||||||
int port = 0;
|
int port = 0;
|
||||||
if (!parseUrl(url, scheme, host, port, path))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// WinHTTP expects the path to include the leading '/'.
|
// split into scheme/host/port/path
|
||||||
HINTERNET hSession = WinHttpOpen(L"MinecraftPE/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
|
if (!parseUrl(url, scheme, host, port, path)) {
|
||||||
if (!hSession)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
HINTERNET hConnect = WinHttpConnect(hSession, std::wstring(host.begin(), host.end()).c_str(), port, 0);
|
|
||||||
if (!hConnect) {
|
|
||||||
WinHttpCloseHandle(hSession);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
HINTERNET hRequest = WinHttpOpenRequest(
|
// creating an http session
|
||||||
hConnect,
|
HINTERNET session = WinHttpOpen(L"MinecraftPE/0.6.1", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
|
||||||
|
if (!session) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
HINTERNET connectHandle = WinHttpConnect(session, std::wstring(host.begin(), host.end()).c_str(), port, 0);
|
||||||
|
if (!connectHandle) {
|
||||||
|
WinHttpCloseHandle(session);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
HINTERNET requestHandle = WinHttpOpenRequest(
|
||||||
|
connectHandle,
|
||||||
L"GET",
|
L"GET",
|
||||||
std::wstring(path.begin(), path.end()).c_str(),
|
std::wstring(path.begin(), path.end()).c_str(),
|
||||||
NULL,
|
NULL,
|
||||||
@@ -64,109 +82,245 @@ bool downloadHttpsWinHttp(const std::string& url, std::vector<unsigned char>& ou
|
|||||||
WINHTTP_FLAG_SECURE
|
WINHTTP_FLAG_SECURE
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!hRequest) {
|
if (!requestHandle) {
|
||||||
WinHttpCloseHandle(hConnect);
|
WinHttpCloseHandle(connectHandle);
|
||||||
WinHttpCloseHandle(hSession);
|
WinHttpCloseHandle(session);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
DWORD redirectPolicy = WINHTTP_OPTION_REDIRECT_POLICY_ALWAYS;
|
DWORD redirectPolicy = WINHTTP_OPTION_REDIRECT_POLICY_ALWAYS;
|
||||||
WinHttpSetOption(hRequest, WINHTTP_OPTION_REDIRECT_POLICY, &redirectPolicy, sizeof(redirectPolicy));
|
WinHttpSetOption(requestHandle, WINHTTP_OPTION_REDIRECT_POLICY, &redirectPolicy, sizeof(redirectPolicy));
|
||||||
|
|
||||||
BOOL result = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
|
BOOL sendResult = WinHttpSendRequest(requestHandle, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
|
||||||
if (!result) {
|
if (!sendResult) {
|
||||||
WinHttpCloseHandle(hRequest);
|
WinHttpCloseHandle(requestHandle);
|
||||||
WinHttpCloseHandle(hConnect);
|
WinHttpCloseHandle(connectHandle);
|
||||||
WinHttpCloseHandle(hSession);
|
WinHttpCloseHandle(session);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
result = WinHttpReceiveResponse(hRequest, NULL);
|
BOOL receiveResult = WinHttpReceiveResponse(requestHandle, NULL);
|
||||||
if (!result) {
|
if (!receiveResult) {
|
||||||
WinHttpCloseHandle(hRequest);
|
WinHttpCloseHandle(requestHandle);
|
||||||
WinHttpCloseHandle(hConnect);
|
WinHttpCloseHandle(connectHandle);
|
||||||
WinHttpCloseHandle(hSession);
|
WinHttpCloseHandle(session);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
DWORD bytesAvailable = 0;
|
DWORD bytesAvailable = 0;
|
||||||
while (WinHttpQueryDataAvailable(hRequest, &bytesAvailable) && bytesAvailable > 0) {
|
while (WinHttpQueryDataAvailable(requestHandle, &bytesAvailable) && bytesAvailable > 0) {
|
||||||
std::vector<unsigned char> buffer(bytesAvailable);
|
std::vector<unsigned char> buffer(bytesAvailable);
|
||||||
DWORD bytesRead = 0;
|
DWORD bytesRead = 0;
|
||||||
if (!WinHttpReadData(hRequest, buffer.data(), bytesAvailable, &bytesRead) || bytesRead == 0)
|
if (!WinHttpReadData(requestHandle, buffer.data(), bytesAvailable, &bytesRead) || bytesRead == 0)
|
||||||
break;
|
break;
|
||||||
outBody.insert(outBody.end(), buffer.begin(), buffer.begin() + bytesRead);
|
outputBody.insert(outputBody.end(), buffer.begin(), buffer.begin() + bytesRead);
|
||||||
}
|
}
|
||||||
|
|
||||||
WinHttpCloseHandle(hRequest);
|
WinHttpCloseHandle(requestHandle);
|
||||||
WinHttpCloseHandle(hConnect);
|
WinHttpCloseHandle(connectHandle);
|
||||||
WinHttpCloseHandle(hSession);
|
WinHttpCloseHandle(session);
|
||||||
|
|
||||||
return !outBody.empty();
|
return !outputBody.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
std::string toLower(const std::string& s) {
|
#if defined(HTTPCLIENT_USE_OPENSSL) && !defined(_WIN32)
|
||||||
std::string out = s;
|
bool downloadHttpsOpenSSL(const std::string& url, std::vector<unsigned char>& outputBody) {
|
||||||
std::transform(out.begin(), out.end(), out.begin(), ::tolower);
|
outputBody.clear();
|
||||||
return out;
|
|
||||||
|
std::string scheme;
|
||||||
|
std::string host;
|
||||||
|
std::string path;
|
||||||
|
int port = 0;
|
||||||
|
|
||||||
|
// split into scheme/host/port/path
|
||||||
|
if (!parseUrl(url, scheme, host, port, path)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int socketFd = -1;
|
||||||
|
if (!resolveAndConnect(host, port, socketFd)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
SSL_library_init();
|
||||||
|
SSL_load_error_strings();
|
||||||
|
OpenSSL_add_all_algorithms();
|
||||||
|
|
||||||
|
SSL_CTX* ctx = SSL_CTX_new(TLS_client_method());
|
||||||
|
if (!ctx) {
|
||||||
|
close(socketFd);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// do not validate certificates we donst ship ca roots.
|
||||||
|
SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, nullptr);
|
||||||
|
|
||||||
|
SSL* ssl = SSL_new(ctx);
|
||||||
|
if (!ssl) {
|
||||||
|
SSL_CTX_free(ctx);
|
||||||
|
close(socketFd);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
SSL_set_fd(ssl, socketFd);
|
||||||
|
SSL_set_tlsext_host_name(ssl, host.c_str());
|
||||||
|
|
||||||
|
if (SSL_connect(ssl) != 1) {
|
||||||
|
SSL_free(ssl);
|
||||||
|
SSL_CTX_free(ctx);
|
||||||
|
close(socketFd);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string httpRequest;
|
||||||
|
httpRequest += "GET ";
|
||||||
|
httpRequest += path;
|
||||||
|
httpRequest += " HTTP/1.1\r\n";
|
||||||
|
httpRequest += "Host: ";
|
||||||
|
httpRequest += host;
|
||||||
|
httpRequest += "\r\n";
|
||||||
|
httpRequest += "User-Agent: MinecraftPE\r\n";
|
||||||
|
httpRequest += "Connection: close\r\n";
|
||||||
|
httpRequest += "\r\n";
|
||||||
|
|
||||||
|
if (SSL_write(ssl, httpRequest.data(), (int)httpRequest.size()) <= 0) {
|
||||||
|
SSL_shutdown(ssl);
|
||||||
|
SSL_free(ssl);
|
||||||
|
SSL_CTX_free(ctx);
|
||||||
|
close(socketFd);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<unsigned char> rawResponse;
|
||||||
|
|
||||||
|
const int BUFFER_SIZE = 4096;
|
||||||
|
unsigned char buffer[BUFFER_SIZE];
|
||||||
|
while (true) {
|
||||||
|
int bytesRead = SSL_read(ssl, buffer, BUFFER_SIZE);
|
||||||
|
if (bytesRead <= 0)
|
||||||
|
break;
|
||||||
|
rawResponse.insert(rawResponse.end(), buffer, buffer + bytesRead);
|
||||||
|
}
|
||||||
|
|
||||||
|
SSL_shutdown(ssl);
|
||||||
|
SSL_free(ssl);
|
||||||
|
SSL_CTX_free(ctx);
|
||||||
|
close(socketFd);
|
||||||
|
|
||||||
|
if (rawResponse.empty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string headerDelimiter = "\r\n\r\n";
|
||||||
|
auto headerEndIt = std::search(rawResponse.begin(), rawResponse.end(), headerDelimiter.begin(), headerDelimiter.end());
|
||||||
|
if (headerEndIt == rawResponse.end()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t headerLength = headerEndIt - rawResponse.begin();
|
||||||
|
std::string headers(reinterpret_cast<const char*>(rawResponse.data()), headerLength);
|
||||||
|
size_t bodyStartIndex = headerLength + headerDelimiter.size();
|
||||||
|
|
||||||
|
int statusCode = 0;
|
||||||
|
if (!extractStatusCode(headers, statusCode)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (statusCode != 200) {
|
||||||
|
std::string bodySnippet;
|
||||||
|
size_t len = rawResponse.size() < 1024 ? rawResponse.size() : 1024;
|
||||||
|
bodySnippet.assign(rawResponse.begin(), rawResponse.begin() + len);
|
||||||
|
LOGW("[HttpClient] HTTP status %d for %s\n", statusCode, url.c_str());
|
||||||
|
LOGW("[HttpClient] Headers:\n%s\n", headers.c_str());
|
||||||
|
LOGW("[HttpClient] Body (up to 1024 bytes):\n%s\n", bodySnippet.c_str());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
outputBody.assign(rawResponse.begin() + bodyStartIndex, rawResponse.end());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
std::string toLower(const std::string& input) {
|
||||||
|
std::string output = input;
|
||||||
|
std::transform(output.begin(), output.end(), output.begin(), ::tolower);
|
||||||
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool parseUrl(const std::string& url, std::string& scheme, std::string& host, int& port, std::string& path) {
|
bool parseUrl(const std::string& url, std::string& schemeOut, std::string& hostOut, int& portOut, std::string& pathOut) {
|
||||||
scheme.clear();
|
schemeOut.clear();
|
||||||
host.clear();
|
hostOut.clear();
|
||||||
path.clear();
|
pathOut.clear();
|
||||||
port = 0;
|
portOut = 0;
|
||||||
|
|
||||||
// Very simple URL parser.
|
size_t schemeSep = url.find("://");
|
||||||
// url format: scheme://host[:port]/path
|
if (schemeSep == std::string::npos) {
|
||||||
auto pos = url.find("://");
|
return false;
|
||||||
if (pos == std::string::npos) return false;
|
}
|
||||||
scheme = toLower(url.substr(0, pos));
|
|
||||||
size_t start = pos + 3;
|
|
||||||
|
|
||||||
size_t slash = url.find('/', start);
|
schemeOut = toLower(url.substr(0, schemeSep));
|
||||||
std::string hostPort = (slash == std::string::npos) ? url.substr(start) : url.substr(start, slash - start);
|
size_t hostStart = schemeSep + 3;
|
||||||
path = (slash == std::string::npos) ? "/" : url.substr(slash);
|
|
||||||
|
|
||||||
size_t colon = hostPort.find(':');
|
// split host/port from the path
|
||||||
if (colon != std::string::npos) {
|
size_t pathStart = url.find('/', hostStart);
|
||||||
host = hostPort.substr(0, colon);
|
std::string hostPort;
|
||||||
port = atoi(hostPort.c_str() + colon + 1);
|
if (pathStart == std::string::npos) {
|
||||||
|
// no path part, so just use / as the default
|
||||||
|
hostPort = url.substr(hostStart);
|
||||||
|
pathOut = "/";
|
||||||
} else {
|
} else {
|
||||||
host = hostPort;
|
hostPort = url.substr(hostStart, pathStart - hostStart);
|
||||||
|
pathOut = url.substr(pathStart);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (scheme == "http") {
|
// if the host includes a ":port", split it out
|
||||||
if (port == 0) port = 80;
|
size_t portSep = hostPort.find(':');
|
||||||
} else if (scheme == "https") {
|
if (portSep != std::string::npos) {
|
||||||
if (port == 0) port = 443;
|
hostOut = hostPort.substr(0, portSep);
|
||||||
|
portOut = atoi(hostPort.c_str() + portSep + 1);
|
||||||
|
} else {
|
||||||
|
hostOut = hostPort;
|
||||||
}
|
}
|
||||||
|
|
||||||
return !host.empty() && !scheme.empty();
|
// fill in default ports for known schemes
|
||||||
|
if (schemeOut == "http") {
|
||||||
|
if (portOut == 0) portOut = 80;
|
||||||
|
} else if (schemeOut == "https") {
|
||||||
|
if (portOut == 0) portOut = 443;
|
||||||
|
}
|
||||||
|
|
||||||
|
// return success only if we got at a scheme and host
|
||||||
|
return !hostOut.empty() && !schemeOut.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool readAll(int sockfd, std::vector<unsigned char>& out) {
|
// read all available data from a tcp socket until the connection is closed
|
||||||
const int BUF_SIZE = 4096;
|
// data is appended to outData
|
||||||
unsigned char buffer[BUF_SIZE];
|
bool readAll(int socketFd, std::vector<unsigned char>& outData) {
|
||||||
|
const int BUFFER_SIZE = 4096;
|
||||||
|
unsigned char buffer[BUFFER_SIZE];
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
int received = recv(sockfd, (char*)buffer, BUF_SIZE, 0);
|
int bytesRead = recv(socketFd, (char*)buffer, BUFFER_SIZE, 0);
|
||||||
if (received <= 0)
|
if (bytesRead <= 0)
|
||||||
break;
|
break;
|
||||||
out.insert(out.end(), buffer, buffer + received);
|
outData.insert(outData.end(), buffer, buffer + bytesRead);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool resolveAndConnect(const std::string& host, int port, int& outSock) {
|
// resolve a hostname and connect a tcp socket to the given host:port
|
||||||
|
// on windows this also makes sure winsock is initialized
|
||||||
|
// if successful, outSocket will contain a connected socket descriptor
|
||||||
|
bool resolveAndConnect(const std::string& host, int port, int& outSocket) {
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
static bool initialized = false;
|
static bool wsaStarted = false;
|
||||||
if (!initialized) {
|
if (!wsaStarted) {
|
||||||
WSADATA wsaData;
|
WSADATA wsaData;
|
||||||
WSAStartup(MAKEWORD(2, 2), &wsaData);
|
WSAStartup(MAKEWORD(2, 2), &wsaData);
|
||||||
initialized = true;
|
wsaStarted = true;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -174,60 +328,95 @@ bool resolveAndConnect(const std::string& host, int port, int& outSock) {
|
|||||||
struct addrinfo* result = NULL;
|
struct addrinfo* result = NULL;
|
||||||
|
|
||||||
memset(&hints, 0, sizeof(hints));
|
memset(&hints, 0, sizeof(hints));
|
||||||
hints.ai_family = AF_UNSPEC;
|
hints.ai_family = AF_UNSPEC; // ipv4 ipv6
|
||||||
hints.ai_socktype = SOCK_STREAM;
|
hints.ai_socktype = SOCK_STREAM; // tcp
|
||||||
|
|
||||||
|
// getaddrinfo expects strings for port and host
|
||||||
std::ostringstream portStream;
|
std::ostringstream portStream;
|
||||||
portStream << port;
|
portStream << port;
|
||||||
const std::string portStr = portStream.str();
|
const std::string portString = portStream.str();
|
||||||
if (getaddrinfo(host.c_str(), portStr.c_str(), &hints, &result) != 0)
|
|
||||||
|
if (getaddrinfo(host.c_str(), portString.c_str(), &hints, &result) != 0) {
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
int sock = -1;
|
int socketFd = -1;
|
||||||
for (struct addrinfo* rp = result; rp != NULL; rp = rp->ai_next) {
|
|
||||||
sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
|
||||||
if (sock < 0) continue;
|
|
||||||
if (connect(sock, rp->ai_addr, (int)rp->ai_addrlen) == 0)
|
|
||||||
break; // success
|
|
||||||
|
|
||||||
|
// try each resolved address until we successfully connect
|
||||||
|
for (struct addrinfo* addr = result; addr != NULL; addr = addr->ai_next) {
|
||||||
|
socketFd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
|
||||||
|
if (socketFd < 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (connect(socketFd, addr->ai_addr, (int)addr->ai_addrlen) == 0) {
|
||||||
|
// connected! yay!
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// failed to connect, try next
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
closesocket(sock);
|
closesocket(socketFd);
|
||||||
#else
|
#else
|
||||||
close(sock);
|
close(socketFd);
|
||||||
#endif
|
#endif
|
||||||
sock = -1;
|
socketFd = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
freeaddrinfo(result);
|
freeaddrinfo(result);
|
||||||
|
|
||||||
if (sock < 0)
|
if (socketFd < 0) {
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
outSock = sock;
|
outSocket = socketFd;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string getHeaderValue(const std::string& headers, const std::string& key) {
|
std::string getHeaderValue(const std::string& headers, const std::string& key) {
|
||||||
std::string lower = toLower(headers);
|
std::string headersLower = toLower(headers);
|
||||||
std::string lowerKey = toLower(key);
|
std::string keyLower = toLower(key);
|
||||||
size_t pos = lower.find(lowerKey);
|
|
||||||
if (pos == std::string::npos) return "";
|
size_t pos = headersLower.find(keyLower);
|
||||||
size_t colon = lower.find(':', pos + lowerKey.size());
|
if (pos == std::string::npos) {
|
||||||
if (colon == std::string::npos) return "";
|
return "";
|
||||||
size_t start = colon + 1;
|
}
|
||||||
while (start < lower.size() && (lower[start] == ' ' || lower[start] == '\t')) start++;
|
|
||||||
size_t end = lower.find('\r', start);
|
size_t colonPos = headersLower.find(':', pos + keyLower.size());
|
||||||
if (end == std::string::npos) end = lower.find('\n', start);
|
if (colonPos == std::string::npos) {
|
||||||
if (end == std::string::npos) end = lower.size();
|
return "";
|
||||||
return headers.substr(start, end - start);
|
}
|
||||||
|
|
||||||
|
size_t valueStart = colonPos + 1;
|
||||||
|
while (valueStart < headersLower.size() && (headersLower[valueStart] == ' ' || headersLower[valueStart] == '\t')) {
|
||||||
|
valueStart++;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t valueEnd = headersLower.find('\r', valueStart);
|
||||||
|
if (valueEnd == std::string::npos) {
|
||||||
|
valueEnd = headersLower.find('\n', valueStart);
|
||||||
|
}
|
||||||
|
if (valueEnd == std::string::npos) {
|
||||||
|
valueEnd = headersLower.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
return headers.substr(valueStart, valueEnd - valueStart);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// wxtract the http status code from the first response line (foex example "HTTP/1.1 200 OK")
|
||||||
|
// returns false on malformed responses.
|
||||||
bool extractStatusCode(const std::string& headers, int& outStatus) {
|
bool extractStatusCode(const std::string& headers, int& outStatus) {
|
||||||
size_t pos = headers.find(" ");
|
size_t firstSpace = headers.find(' ');
|
||||||
if (pos == std::string::npos) return false;
|
if (firstSpace == std::string::npos) {
|
||||||
size_t pos2 = headers.find(" ", pos + 1);
|
return false;
|
||||||
if (pos2 == std::string::npos) return false;
|
}
|
||||||
std::string code = headers.substr(pos + 1, pos2 - pos - 1);
|
|
||||||
|
size_t secondSpace = headers.find(' ', firstSpace + 1);
|
||||||
|
if (secondSpace == std::string::npos) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string code = headers.substr(firstSpace + 1, secondSpace - firstSpace - 1);
|
||||||
outStatus = atoi(code.c_str());
|
outStatus = atoi(code.c_str());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -239,96 +428,121 @@ namespace HttpClient {
|
|||||||
bool download(const std::string& url, std::vector<unsigned char>& outBody) {
|
bool download(const std::string& url, std::vector<unsigned char>& outBody) {
|
||||||
outBody.clear();
|
outBody.clear();
|
||||||
|
|
||||||
std::string currentUrl = url;
|
// keep a copy of the current url so we can follow redirects
|
||||||
for (int redirect = 0; redirect < 3; ++redirect) {
|
std::string urlToDownload = url;
|
||||||
std::string scheme, host, path;
|
|
||||||
int port = 0;
|
// follow up to 3 redirects (301/302/307/308)
|
||||||
if (!parseUrl(currentUrl, scheme, host, port, path)) {
|
// if a redirect is encountered we loop again with the new loaction url
|
||||||
LOGW("[HttpClient] parseUrl failed for '%s'\n", currentUrl.c_str());
|
for (int redirectCount = 0; redirectCount < 3; ++redirectCount) {
|
||||||
|
std::string urlScheme;
|
||||||
|
std::string urlHost;
|
||||||
|
std::string urlPath;
|
||||||
|
int urlPort = 0;
|
||||||
|
|
||||||
|
// parse the url into its components so we can open a socket/start an https request
|
||||||
|
if (!parseUrl(urlToDownload, urlScheme, urlHost, urlPort, urlPath)) {
|
||||||
|
LOGW("[HttpClient] parseUrl failed for '%s'\n", urlToDownload.c_str());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (scheme == "https") {
|
// for https we delegate to winhttp on windows, since this simple client
|
||||||
|
// only supports plain http over raw sockets
|
||||||
|
if (urlScheme == "https") {
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
LOGI("[HttpClient] using WinHTTP for HTTPS URL %s\n", currentUrl.c_str());
|
LOGI("[HttpClient] using WinHTTP for HTTPS URL %s\n", urlToDownload.c_str());
|
||||||
return downloadHttpsWinHttp(currentUrl, outBody);
|
return downloadHttpsWinHttp(urlToDownload, outBody);
|
||||||
|
#elif defined(HTTPCLIENT_USE_OPENSSL)
|
||||||
|
LOGI("[HttpClient] using OpenSSL for HTTPS URL %s\n", urlToDownload.c_str());
|
||||||
|
return downloadHttpsOpenSSL(urlToDownload, outBody);
|
||||||
#else
|
#else
|
||||||
LOGW("[HttpClient] HTTPS not supported on this platform: %s\n", currentUrl.c_str());
|
LOGW("[HttpClient] HTTPS not supported on this platform: %s\n", urlToDownload.c_str());
|
||||||
return false;
|
return false;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
if (scheme != "http") {
|
// we only support plain http for all non-windows platforms for nw
|
||||||
LOGW("[HttpClient] unsupported scheme '%s' for URL '%s'\n", scheme.c_str(), currentUrl.c_str());
|
if (urlScheme != "http") {
|
||||||
|
LOGW("[HttpClient] unsupported scheme '%s' for URL '%s'\n", urlScheme.c_str(), urlToDownload.c_str());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int sock = -1;
|
int socketFd = -1;
|
||||||
if (!resolveAndConnect(host, port, sock)) {
|
if (!resolveAndConnect(urlHost, urlPort, socketFd)) {
|
||||||
LOGW("[HttpClient] resolve/connect failed for %s:%d\n", host.c_str(), port);
|
LOGW("[HttpClient] resolve/connect failed for %s:%d\n", urlHost.c_str(), urlPort);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string request = "GET " + path + " HTTP/1.1\r\n";
|
std::string httpRequest;
|
||||||
request += "Host: " + host + "\r\n";
|
httpRequest += "GET ";
|
||||||
request += "User-Agent: MinecraftPE\r\n";
|
httpRequest += urlPath;
|
||||||
request += "Connection: close\r\n";
|
httpRequest += " HTTP/1.1\r\n";
|
||||||
request += "\r\n";
|
httpRequest += "Host: ";
|
||||||
|
httpRequest += urlHost;
|
||||||
|
httpRequest += "\r\n";
|
||||||
|
httpRequest += "User-Agent: MinecraftPE\r\n";
|
||||||
|
httpRequest += "Connection: close\r\n";
|
||||||
|
httpRequest += "\r\n";
|
||||||
|
|
||||||
send(sock, request.c_str(), (int)request.size(), 0);
|
send(socketFd, httpRequest.c_str(), (int)httpRequest.size(), 0);
|
||||||
|
|
||||||
std::vector<unsigned char> raw;
|
std::vector<unsigned char> rawResponse;
|
||||||
readAll(sock, raw);
|
readAll(socketFd, rawResponse);
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
closesocket(sock);
|
closesocket(socketFd);
|
||||||
#else
|
#else
|
||||||
close(sock);
|
close(socketFd);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (raw.empty()) {
|
if (rawResponse.empty()) {
|
||||||
LOGW("[HttpClient] no response data from %s\n", currentUrl.c_str());
|
LOGW("[HttpClient] no response data from %s\n", urlToDownload.c_str());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// split headers and body
|
// find the end of the headers (\r\n\r\n) so we can split headers and body
|
||||||
const std::string delim = "\r\n\r\n";
|
const std::string headerDelimiter = "\r\n\r\n";
|
||||||
auto it = std::search(raw.begin(), raw.end(), delim.begin(), delim.end());
|
auto headerEndIt = std::search(rawResponse.begin(), rawResponse.end(), headerDelimiter.begin(), headerDelimiter.end());
|
||||||
if (it == raw.end())
|
if (headerEndIt == rawResponse.end()) {
|
||||||
|
// we didn't find the end of headers :(
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
size_t headerLen = it - raw.begin();
|
// extract the header block as a string so we can inspect it
|
||||||
std::string headers(reinterpret_cast<const char*>(raw.data()), headerLen);
|
size_t headerLength = headerEndIt - rawResponse.begin();
|
||||||
size_t bodyStart = headerLen + delim.size();
|
std::string headers(reinterpret_cast<const char*>(rawResponse.data()), headerLength);
|
||||||
|
size_t bodyStartIndex = headerLength + headerDelimiter.size();
|
||||||
|
|
||||||
int status = 0;
|
int statusCode = 0;
|
||||||
if (!extractStatusCode(headers, status))
|
if (!extractStatusCode(headers, statusCode)) {
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (status == 301 || status == 302 || status == 307 || status == 308) {
|
if (statusCode == 301 || statusCode == 302 || statusCode == 307 || statusCode == 308) {
|
||||||
std::string location = getHeaderValue(headers, "Location");
|
std::string location = getHeaderValue(headers, "Location");
|
||||||
if (location.empty()) {
|
if (location.empty()) {
|
||||||
LOGW("[HttpClient] redirect without Location header for %s\n", currentUrl.c_str());
|
LOGW("[HttpClient] redirect without Location header for %s\n", urlToDownload.c_str());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
LOGI("[HttpClient] redirect %s -> %s\n", currentUrl.c_str(), location.c_str());
|
LOGI("[HttpClient] redirect %s -> %s\n", urlToDownload.c_str(), location.c_str());
|
||||||
currentUrl = location;
|
urlToDownload = location;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (status != 200) {
|
if (statusCode != 200) {
|
||||||
|
// if we got any status other than 200 OK, log what happened
|
||||||
std::string bodySnippet;
|
std::string bodySnippet;
|
||||||
if (!outBody.empty()) {
|
if (!outBody.empty()) {
|
||||||
size_t len = outBody.size() < 1024 ? outBody.size() : 1024;
|
size_t len = outBody.size() < 1024 ? outBody.size() : 1024;
|
||||||
bodySnippet.assign(outBody.begin(), outBody.begin() + len);
|
bodySnippet.assign(outBody.begin(), outBody.begin() + len);
|
||||||
}
|
}
|
||||||
LOGW("[HttpClient] HTTP status %d for %s\n", status, currentUrl.c_str());
|
LOGW("[HttpClient] HTTP status %d for %s\n", statusCode, urlToDownload.c_str());
|
||||||
LOGW("[HttpClient] Headers:\n%s\n", headers.c_str());
|
LOGW("[HttpClient] Headers:\n%s\n", headers.c_str());
|
||||||
LOGW("[HttpClient] Body (up to 1024 bytes):\n%s\n", bodySnippet.c_str());
|
LOGW("[HttpClient] Body (up to 1024 bytes):\n%s\n", bodySnippet.c_str());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
outBody.assign(raw.begin() + bodyStart, raw.end());
|
// everything looks good! copy just the body bytes (after the headers) into outBody.
|
||||||
|
outBody.assign(rawResponse.begin() + bodyStartIndex, rawResponse.end());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,9 +6,6 @@
|
|||||||
|
|
||||||
namespace HttpClient {
|
namespace HttpClient {
|
||||||
|
|
||||||
/// Download the given URL into "outBody".
|
|
||||||
/// Returns true if the download completed successfully (HTTP 200) and the body is in outBody.
|
|
||||||
/// This function supports plain HTTP only (no TLS). It will follow up to 3 redirects.
|
|
||||||
bool download(const std::string& url, std::vector<unsigned char>& outBody);
|
bool download(const std::string& url, std::vector<unsigned char>& outBody);
|
||||||
|
|
||||||
} // namespace HttpClient
|
} // namespace HttpClient
|
||||||
|
|||||||
@@ -56,6 +56,7 @@ public:
|
|||||||
static const int KEY_Z = 90;
|
static const int KEY_Z = 90;
|
||||||
|
|
||||||
static const int KEY_BACKSPACE = 8;
|
static const int KEY_BACKSPACE = 8;
|
||||||
|
static const int KEY_TAB = 9;
|
||||||
static const int KEY_RETURN = 13;
|
static const int KEY_RETURN = 13;
|
||||||
|
|
||||||
static const int KEY_F1 = 112;
|
static const int KEY_F1 = 112;
|
||||||
|
|||||||
Reference in New Issue
Block a user