oatpp/web/client/HttpRequestExecutor.cpp

228 lines
11 KiB
C++
Raw Normal View History

/***************************************************************************
*
* Project _____ __ ____ _ _
* ( _ ) /__\ (_ _)_| |_ _| |_
* )(_)( /(__)\ )( (_ _)(_ _)
* (_____)(__)(__)(__) |_| |_|
*
*
* Copyright 2018-present, Leonid Stryzhevskyi, <lganzzzo@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************************/
#include "HttpRequestExecutor.hpp"
2018-12-08 00:29:01 +08:00
#include "oatpp/web/protocol/http/incoming/ResponseHeadersReader.hpp"
2018-03-29 12:50:20 +08:00
#include "oatpp/web/protocol/http/outgoing/Request.hpp"
#include "oatpp/web/protocol/http/outgoing/BufferBody.hpp"
2018-03-29 12:50:20 +08:00
#include "oatpp/network/Connection.hpp"
#include "oatpp/core/data/stream/ChunkedBuffer.hpp"
#include "oatpp/core/data/stream/StreamBufferedProxy.hpp"
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
namespace oatpp { namespace web { namespace client {
2018-11-11 06:51:56 +08:00
std::shared_ptr<HttpRequestExecutor::ConnectionHandle> HttpRequestExecutor::getConnection() {
auto connection = m_connectionProvider->getConnection();
if(!connection){
throw RequestExecutionError(RequestExecutionError::ERROR_CODE_CANT_CONNECT,
"[oatpp::web::client::HttpRequestExecutor::getConnection()]: ConnectionProvider failed to provide Connection");
}
return std::make_shared<HttpConnectionHandle>(connection);
}
HttpRequestExecutor::Action HttpRequestExecutor::getConnectionAsync(oatpp::async::AbstractCoroutine* parentCoroutine, AsyncConnectionCallback callback) {
class GetConnectionCoroutine : public oatpp::async::CoroutineWithResult<GetConnectionCoroutine, std::shared_ptr<ConnectionHandle>> {
private:
std::shared_ptr<oatpp::network::ClientConnectionProvider> m_connectionProvider;
public:
GetConnectionCoroutine(const std::shared_ptr<oatpp::network::ClientConnectionProvider>& connectionProvider)
: m_connectionProvider(connectionProvider)
{}
Action act() override {
oatpp::network::ClientConnectionProvider::AsyncCallback callback =
static_cast<oatpp::network::ClientConnectionProvider::AsyncCallback>(&GetConnectionCoroutine::onConnectionReady);
return m_connectionProvider->getConnectionAsync(this, callback);
}
Action onConnectionReady(const std::shared_ptr<oatpp::data::stream::IOStream>& connection) {
return _return(std::make_shared<HttpConnectionHandle>(connection));
}
};
return parentCoroutine->startCoroutineForResult<GetConnectionCoroutine>(callback, m_connectionProvider);
}
std::shared_ptr<HttpRequestExecutor::Response>
2018-06-26 07:30:01 +08:00
HttpRequestExecutor::execute(const String& method,
const String& path,
2018-12-08 00:29:01 +08:00
const Headers& headers,
2018-11-11 06:51:56 +08:00
const std::shared_ptr<Body>& body,
const std::shared_ptr<ConnectionHandle>& connectionHandle) {
std::shared_ptr<oatpp::network::ConnectionProvider::IOStream> connection;
if(connectionHandle) {
connection = static_cast<HttpConnectionHandle*>(connectionHandle.get())->connection;
} else {
connection = m_connectionProvider->getConnection();
}
if(!connection){
throw RequestExecutionError(RequestExecutionError::ERROR_CODE_CANT_CONNECT,
"[oatpp::web::client::HttpRequestExecutor::execute()]: ConnectionProvider failed to provide Connection");
}
auto request = oatpp::web::protocol::http::outgoing::Request::createShared(method, path, headers, body);
2018-12-08 00:29:01 +08:00
request->putHeaderIfNotExists(oatpp::web::protocol::http::Header::HOST, m_connectionProvider->getProperty("host"));
request->putHeaderIfNotExists(oatpp::web::protocol::http::Header::CONNECTION, oatpp::web::protocol::http::Header::Value::CONNECTION_KEEP_ALIVE);
auto ioBuffer = oatpp::data::buffer::IOBuffer::createShared();
auto upStream = oatpp::data::stream::OutputStreamBufferedProxy::createShared(connection, ioBuffer);
request->send(upStream);
upStream->flush();
2018-12-08 00:29:01 +08:00
oatpp::web::protocol::http::incoming::ResponseHeadersReader headerReader(ioBuffer->getData(), ioBuffer->getSize(), 4096);
oatpp::web::protocol::http::HttpError::Info error;
2018-12-08 00:29:01 +08:00
const auto& result = headerReader.readHeaders(connection, error);
if(error.status.code != 0) {
2018-12-08 00:29:01 +08:00
throw RequestExecutionError(RequestExecutionError::ERROR_CODE_CANT_PARSE_STARTING_LINE,
"[oatpp::web::client::HttpRequestExecutor::execute()]: Failed to parse response. Invalid response headers");
}
if(error.ioStatus < 0) {
throw RequestExecutionError(RequestExecutionError::ERROR_CODE_CANT_PARSE_STARTING_LINE,
"[oatpp::web::client::HttpRequestExecutor::execute()]: Failed to read response.");
}
auto bodyStream = oatpp::data::stream::InputStreamBufferedProxy::createShared(connection,
ioBuffer,
2018-12-08 00:29:01 +08:00
result.bufferPosStart,
result.bufferPosEnd);
2018-12-08 00:29:01 +08:00
return Response::createShared(result.startingLine.statusCode,
result.startingLine.description.toString(),
result.headers, bodyStream, m_bodyDecoder);
}
oatpp::async::Action HttpRequestExecutor::executeAsync(oatpp::async::AbstractCoroutine* parentCoroutine,
AsyncCallback callback,
2018-06-26 07:30:01 +08:00
const String& method,
const String& path,
2018-12-08 00:29:01 +08:00
const Headers& headers,
2018-11-11 06:51:56 +08:00
const std::shared_ptr<Body>& body,
const std::shared_ptr<ConnectionHandle>& connectionHandle) {
2018-12-08 00:29:01 +08:00
typedef protocol::http::incoming::ResponseHeadersReader ResponseHeadersReader;
class ExecutorCoroutine : public oatpp::async::CoroutineWithResult<ExecutorCoroutine, std::shared_ptr<HttpRequestExecutor::Response>> {
private:
2018-04-18 06:33:19 +08:00
std::shared_ptr<oatpp::network::ClientConnectionProvider> m_connectionProvider;
2018-06-26 07:30:01 +08:00
String m_method;
String m_path;
2018-12-08 00:29:01 +08:00
Headers m_headers;
std::shared_ptr<Body> m_body;
std::shared_ptr<const oatpp::web::protocol::http::incoming::BodyDecoder> m_bodyDecoder;
2018-11-11 06:51:56 +08:00
std::shared_ptr<ConnectionHandle> m_connectionHandle;
private:
std::shared_ptr<oatpp::data::stream::IOStream> m_connection;
2018-04-18 06:33:19 +08:00
std::shared_ptr<oatpp::data::buffer::IOBuffer> m_ioBuffer;
void* m_bufferPointer;
os::io::Library::v_size m_bufferBytesLeftToRead;
public:
2018-04-18 06:33:19 +08:00
ExecutorCoroutine(const std::shared_ptr<oatpp::network::ClientConnectionProvider>& connectionProvider,
2018-06-26 07:30:01 +08:00
const String& method,
const String& path,
2018-12-08 00:29:01 +08:00
const Headers& headers,
const std::shared_ptr<Body>& body,
2018-11-11 06:51:56 +08:00
const std::shared_ptr<const oatpp::web::protocol::http::incoming::BodyDecoder>& bodyDecoder,
const std::shared_ptr<ConnectionHandle>& connectionHandle)
2018-04-18 06:33:19 +08:00
: m_connectionProvider(connectionProvider)
, m_method(method)
, m_path(path)
, m_headers(headers)
, m_body(body)
, m_bodyDecoder(bodyDecoder)
2018-11-11 06:51:56 +08:00
, m_connectionHandle(connectionHandle)
{}
Action act() override {
2018-11-11 06:51:56 +08:00
if(m_connectionHandle) {
/* Careful here onConnectionReady() should have only one possibe state */
/* Because it is called here in synchronous manner */
return onConnectionReady(static_cast<HttpConnectionHandle*>(m_connectionHandle.get())->connection);
} else {
oatpp::network::ClientConnectionProvider::AsyncCallback callback =
static_cast<oatpp::network::ClientConnectionProvider::AsyncCallback>(&ExecutorCoroutine::onConnectionReady);
return m_connectionProvider->getConnectionAsync(this, callback);
}
2018-04-18 06:33:19 +08:00
}
2018-11-11 06:51:56 +08:00
/* Careful here onConnectionReady() should have only one possibe state */
/* Because there is a call to it from act() in synchronous manner */
2018-04-18 06:33:19 +08:00
Action onConnectionReady(const std::shared_ptr<oatpp::data::stream::IOStream>& connection) {
m_connection = connection;
auto request = oatpp::web::protocol::http::outgoing::Request::createShared(m_method, m_path, m_headers, m_body);
2018-12-08 00:29:01 +08:00
request->putHeaderIfNotExists(Header::HOST, m_connectionProvider->getProperty("host"));
request->putHeaderIfNotExists(Header::CONNECTION, Header::Value::CONNECTION_KEEP_ALIVE);
2018-04-18 06:33:19 +08:00
m_ioBuffer = oatpp::data::buffer::IOBuffer::createShared();
auto upStream = oatpp::data::stream::OutputStreamBufferedProxy::createShared(connection, m_ioBuffer);
m_bufferPointer = m_ioBuffer->getData();
m_bufferBytesLeftToRead = m_ioBuffer->getSize();
return request->sendAsync(this, upStream->flushAsync(this, yieldTo(&ExecutorCoroutine::readResponse)), upStream);
}
Action readResponse() {
2018-12-08 00:29:01 +08:00
ResponseHeadersReader::AsyncCallback callback = static_cast<ResponseHeadersReader::AsyncCallback>(&ExecutorCoroutine::onHeadersParsed);
ResponseHeadersReader headersReader(m_ioBuffer->getData(), m_ioBuffer->getSize(), 4096);
return headersReader.readHeadersAsync(this, callback, m_connection);
2018-04-18 06:33:19 +08:00
}
2018-12-08 00:29:01 +08:00
Action onHeadersParsed(const ResponseHeadersReader::Result& result) {
2018-04-18 06:33:19 +08:00
2018-12-08 00:29:01 +08:00
auto bodyStream = oatpp::data::stream::InputStreamBufferedProxy::createShared(m_connection,
m_ioBuffer,
result.bufferPosStart,
result.bufferPosEnd);
2018-04-18 06:33:19 +08:00
2018-12-08 00:29:01 +08:00
return _return(Response::createShared(result.startingLine.statusCode,
result.startingLine.description.toString(),
result.headers, bodyStream, m_bodyDecoder));
2018-04-18 06:33:19 +08:00
}
};
2018-11-11 06:51:56 +08:00
return parentCoroutine->startCoroutineForResult<ExecutorCoroutine>(callback, m_connectionProvider, method, path, headers, body, m_bodyDecoder, connectionHandle);
}
}}}