Giter VIP home page Giter VIP logo

Comments (5)

megabyte1024 avatar megabyte1024 commented on June 19, 2024

Could you please to change also the the server::connection::write_response method.

from websocketpp.

zaphoyd avatar zaphoyd commented on June 19, 2024

The last commit should fix the problem from a correctness perspective. I am leaving this issue open for the moment because this solution involves an awful lot of copying that I want to try and avoid. I think the protocol processors should be able to return a reference to an appropriate shared buffer rather than returning a value that gets copied into a shared buffer locally.

from websocketpp.

megabyte1024 avatar megabyte1024 commented on June 19, 2024

I have done a "homework" and had realized that it's enough to use members strings as a storage for requests and responses. Also I changed the raw method to pass a string reference as input parameter, rather to return the string. It looks not very nice but saves one string copy operation on old STL implementations without the "Rvalue references" feature of C++11 and does not require creating string on the heap using std::shared_ptr inside of the raw method. Here is a patch

File: client.hpp
------------------------------------------------- cut here -------------------------------------------------
-#include "../shared_const_buffer.hpp"
------------------------------------------------- cut here -------------------------------------------------
std::string m_handshake_key;
http::parser::request m_request;

  •           std::string                                 m_request_data;
     http::parser::response      m_response;
    
    ------------------------------------------------- cut here -------------------------------------------------
    // TODO: generating this raw request involves way too much copying in cases
    // without string/vector move semantics.

- shared_const_buffer buffer(m_request.raw());

- //std::string raw = m_request.raw();

- //m_endpoint.alog().at(log::alevel::DEBUG_HANDSHAKE) << raw << log::endl;

  • m_request.raw(m_request_data);

  • boost::asio::async_write(
    m_connection.get_socket(),

  •    //boost::asio::buffer(raw),
    
  •    buffer,
    
  •    boost::asio::buffer(m_request_data),
     boost::bind(
         &type::handle_write_request,
         m_connection.shared_from_this(),
    

    ------------------------------------------------- cut here -------------------------------------------------
    throw http::exception("Could not parse server response.",http::status_code::BAD_REQUEST);
    }

  •    m_endpoint.alog().at(log::alevel::DEBUG_HANDSHAKE) << m_response.raw() << log::endl;
    
  •           std::string response;
    
  •           m_response.raw(response);
    
  •    m_endpoint.alog().at(log::alevel::DEBUG_HANDSHAKE) << response << log::endl;
    
     // error checking
     if (m_response.get_status_code() != http::status_code::SWITCHING_PROTOCOLS) {
    

    ------------------------------------------------- cut here -------------------------------------------------

to be continued...

from websocketpp.

megabyte1024 avatar megabyte1024 commented on June 19, 2024

File: server.hpp
------------------------------------------------- cut here -------------------------------------------------
-#include "../shared_const_buffer.hpp"
------------------------------------------------- cut here -------------------------------------------------
http::parser::request m_request;
http::parser::response m_response;

  •           std::string                                 m_response_data;
     blank_rng                   m_rng;
    

    ------------------------------------------------- cut here -------------------------------------------------
    m_response.replace_header("Server","WebSocket++/0.2.0");

  • std::string raw = m_response.raw();

  • m_response.raw(m_response_data);

// Hack for legacy HyBi
if (m_version == 0) {

  •    raw += boost::dynamic_pointer_castprocessor::hybi_legacy<connection_type >(m_connection.m_processor)->get_key3();
    
  •    m_response_data += boost::dynamic_pointer_cast<processor::hybi_legacy<connection_type> >(m_connection.m_processor)->get_key3();
    

    }

  • m_endpoint.alog().at(log::alevel::DEBUG_HANDSHAKE) << raw << log::endl;

  • m_endpoint.alog().at(log::alevel::DEBUG_HANDSHAKE) << m_response_data << log::endl;

boost::asio::async_write(
m_connection.get_socket(),

  •    boost::asio::buffer(raw),
    
  •    boost::asio::buffer(m_response_data),
     boost::bind(
         &type::handle_write_response,
         m_connection.shared_from_this(),
    
    ------------------------------------------------- cut here -------------------------------------------------

from websocketpp.

megabyte1024 avatar megabyte1024 commented on June 19, 2024

Here the last part. A few mistypings are also fixed. Additionally I used to use constant methods where is possible. So I added the const word in a couple of places.

File: Parser.hpp
------------------------------------------------- cut here -------------------------------------------------

  •    throw "No Implimented";
    
  •    throw "Not Implemented";
    
    ------------------------------------------------- cut here -------------------------------------------------
  • std::string raw_headers() {
  • std::string raw_headers() const {
    std::stringstream raw;
  •    header_list::iterator it;
    
  •    for (it = m_headers.begin(); it != m_headers.end(); it++) {
    
  •    header_list::const_iterator it;
    
  •    for (it = m_headers.cbegin(); it != m_headers.cend(); it++) {
         raw << it->first << ": " << it->second << "\r\n";
     }
    
    ------------------------------------------------- cut here -------------------------------------------------
    // TODO: validation. Make sure all required fields have been set?
  • std::string raw() {
  • void raw(std::string &data) const {
    std::stringstream raw;
 raw << m_method << " " << m_uri << " " << version() << "\r\n";
 raw << raw_headers() << "\r\n";
  •    return raw.str();
    
  •    data = raw.str();
    
    }
    ------------------------------------------------- cut here -------------------------------------------------
    // TODO: validation. Make sure all required fields have been set?
  • std::string raw() {
  • void raw(std::string &data) const {
    std::stringstream raw;
 raw << version() << " " << m_status_code << " " << m_status_msg << "\r\n";

@@ -237,7 +237,7 @@ public:

 raw << m_body;
  •    return raw.str();
    
  •    data = raw.str();
    
    }
    ------------------------------------------------- cut here -------------------------------------------------

from websocketpp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.