Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Host on device via webserver instead of using file://
  • Loading branch information
nitanmarcel committed Apr 4, 2022
1 parent 5d8dbff commit 3b9e50c
Show file tree
Hide file tree
Showing 37 changed files with 3,513 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
@@ -0,0 +1,3 @@
[submodule "libs/http-parser"]
path = libs/http-parser
url = https://github.com/nodejs/http-parser.git
7 changes: 5 additions & 2 deletions CMakeLists.txt
Expand Up @@ -3,7 +3,9 @@ project(cinny C CXX)

# Automatically create moc files
set(CMAKE_AUTOMOC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD 14)

include(GNUInstallDirs)

find_package(Qt5Core REQUIRED)
find_package(Qt5Qml REQUIRED)
Expand Down Expand Up @@ -52,7 +54,7 @@ qt5_add_resources(QT_RESOURCES qml/qml.qrc)
qt5_add_resources(QT_RESOURCES assets/assets.qrc)
add_executable(${PROJECT_NAME} main.cpp ${QT_RESOURCES})
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS_RELEASE -s)
target_link_libraries(${PROJECT_NAME} Qt5::Gui Qt5::Qml Qt5::Quick Qt5::QuickControls2)
target_link_libraries(${PROJECT_NAME} QHttp Qt5::Gui Qt5::Qml Qt5::Quick Qt5::QuickControls2)
install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX})

# Translations
Expand All @@ -79,6 +81,7 @@ install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${DESKTOP_FILE_NAME} DESTINATION ${DAT
add_subdirectory(po)
add_subdirectory(plugins)
add_subdirectory(push)
add_subdirectory(libs)

# Make source files visible in qtcreator
file(GLOB_RECURSE PROJECT_SRC_FILES
Expand Down
1 change: 1 addition & 0 deletions libs/CMakeLists.txt
@@ -0,0 +1 @@
add_subdirectory(qhttp)
1 change: 1 addition & 0 deletions libs/http-parser
Submodule http-parser added at ec8b5e
57 changes: 57 additions & 0 deletions libs/qhttp/CMakeLists.txt
@@ -0,0 +1,57 @@
project(qhttp C CXX)
cmake_minimum_required(VERSION 3.0.0)


find_package(Qt5Core REQUIRED)
find_package(Qt5Network REQUIRED)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(QHTTP "QHttp")

set(SRC
../http-parser/http_parser.c
../http-parser/http_parser.h
qhttpabstracts.cpp
qhttpabstracts.hpp
qhttpfwd.hpp
qhttpserver.cpp
qhttpserver.hpp
qhttpserverconnection.cpp
qhttpserverconnection.hpp
qhttpserverrequest.cpp
qhttpserverrequest.hpp
qhttpserverresponse.cpp
qhttpserverresponse.hpp
)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)

add_library(${QHTTP} SHARED ${SRC})



set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

target_link_libraries(${QHTTP} PUBLIC
Qt5::Core
Qt5::Network
)

target_include_directories(${QHTTP} PUBLIC
.
../
)

target_compile_definitions(${QHTTP} PUBLIC
QHTTP_DYNAMIC_LIB
QHTTP_HAS_CLIENT
QHTTP_MEMORY_LOG=0
)

install(TARGETS ${QHTTP} LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
)

22 changes: 22 additions & 0 deletions libs/qhttp/LICENSE
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2014 Amir Zamani

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.


119 changes: 119 additions & 0 deletions libs/qhttp/private/httpparser.hxx
@@ -0,0 +1,119 @@
/** @file httpparser.hxx
*
* @copyright (C) 2016
* @date 2016.05.26
* @version 1.0.0
* @author amir zamani <azadkuh@live.com>
*
*/

#ifndef __QHTTP_HTTPPARSER_HXX__
#define __QHTTP_HTTPPARSER_HXX__

#include "qhttpbase.hpp"
///////////////////////////////////////////////////////////////////////////////
namespace qhttp {
namespace details {
///////////////////////////////////////////////////////////////////////////////


/// base HttpParser based on joyent http_parser
template<class TImpl>
class HttpParser
{
public:
explicit HttpParser(http_parser_type type) {
// create http_parser object
iparser.data = static_cast<TImpl*>(this);
http_parser_init(&iparser, type);

memset(&iparserSettings, 0, sizeof(http_parser_settings));
iparserSettings.on_message_begin = onMessageBegin;
iparserSettings.on_url = onUrl;
iparserSettings.on_status = onStatus;
iparserSettings.on_header_field = onHeaderField;
iparserSettings.on_header_value = onHeaderValue;
iparserSettings.on_headers_complete = onHeadersComplete;
iparserSettings.on_body = onBody;
iparserSettings.on_message_complete = onMessageComplete;
}

size_t parse(const char* data, size_t length) {
return http_parser_execute(&iparser,
&iparserSettings,
data,
length);
}

public: // callback functions for http_parser_settings
static int onMessageBegin(http_parser* p) {
return me(p)->messageBegin(p);
}

static int onUrl(http_parser* p, const char* at, size_t length) {
return me(p)->url(p, at, length);
}

static int onStatus(http_parser* p, const char* at, size_t length) {
return me(p)->status(p, at, length);
}

static int onHeaderField(http_parser* p, const char* at, size_t length) {
return me(p)->headerField(p, at, length);
}

static int onHeaderValue(http_parser* p, const char* at, size_t length) {
return me(p)->headerValue(p, at, length);
}

static int onHeadersComplete(http_parser* p) {
return me(p)->headersComplete(p);
}

static int onBody(http_parser* p, const char* at, size_t length) {
return me(p)->body(p, at, length);
}

static int onMessageComplete(http_parser* p) {
return me(p)->messageComplete(p);
}


protected:
// The ones we are reading in from the parser
QByteArray itempHeaderField;
QByteArray itempHeaderValue;
// if connection has a timeout, these fields will be used
quint32 itimeOut = 0;
QBasicTimer itimer;
// uniform socket object
QSocket isocket;
// if connection should persist
bool ikeepAlive = false;

// joyent http_parser
http_parser iparser;
http_parser_settings iparserSettings;

static auto me(http_parser* p) {
return static_cast<TImpl*>(p->data);
}
}; //

/// basic request parser (server)
template<class TImpl>
struct HttpRequestParser : public HttpParser<TImpl> {
HttpRequestParser() : HttpParser<TImpl>(HTTP_REQUEST) {}
};

/// basic response parser (clinet)
template<class TImpl>
struct HttpResponseParser : public HttpParser<TImpl> {
HttpResponseParser() : HttpParser<TImpl>(HTTP_RESPONSE) {}
};

///////////////////////////////////////////////////////////////////////////////
} // namespace details
} // namespace qhttp
///////////////////////////////////////////////////////////////////////////////
#endif // __QHTTP_HTTPPARSER_HXX__
78 changes: 78 additions & 0 deletions libs/qhttp/private/httpreader.hxx
@@ -0,0 +1,78 @@
/** @file httpreader.hxx
*
* @copyright (C) 2016
* @date 2016.05.26
* @version 1.0.0
* @author amir zamani <azadkuh@live.com>
*
*/

#ifndef __QHTTP_HTTPREADER_HXX__
#define __QHTTP_HTTPREADER_HXX__

#include "qhttpbase.hpp"
///////////////////////////////////////////////////////////////////////////////
namespace qhttp {
namespace details {
///////////////////////////////////////////////////////////////////////////////

// usage in client::QHttpResponse, server::QHttpRequest
template<class TBase>
class HttpReader : public TBase
{
public:
enum TReadState {
EEmpty,
EPartial,
EComplete,
ESent
};

public:
void collectData(int atMost) {
icollectRequired = true;
icollectCapacity = atMost;
icollectedData.clear();
if ( atMost > 0 )
icollectedData.reserve(atMost);
}

bool append(const char* data, size_t length) {
if ( !icollectRequired ) // not allowed to collect data
return false;

int newLength = icollectedData.length() + (int) length;

if ( icollectCapacity > 0 && newLength > icollectCapacity )
return false; // the capacity is full

icollectedData.append(data, length);
return true;
}

// call cb if the message is not finalized yet
template<class Func>
void finalizeSending(Func cb) {
if ( ireadState != EComplete ) {
ireadState = EComplete;
isuccessful = true;

cb();
}
}

public:
bool isuccessful = false;
TReadState ireadState = EEmpty;

/// shall I collect incoming body data by myself?
bool icollectRequired = false;
int icollectCapacity = 0;
QByteArray icollectedData;
};

///////////////////////////////////////////////////////////////////////////////
} // namespace details
} // namespace qhttp
///////////////////////////////////////////////////////////////////////////////
#endif // __QHTTP_HTTPREADER_HXX__

0 comments on commit 3b9e50c

Please sign in to comment.