ipkn / crow

Crow is very fast and easy to use C++ micro web framework (inspired by Python Flask)
BSD 3-Clause "New" or "Revised" License
7.47k stars 888 forks source link

Is this possible to write bytes to response? #306

Open 48cf opened 6 years ago

48cf commented 6 years ago

All I saw is able to use with crow::response::write is a std::string. I would like to be able to write raw bytes to my response. Is this possible? If so, how am I able to do that?

mrozigor commented 6 years ago

Can't you just create string from bytes (with constructor)? If you want to send raw bytes, then you should probably use sockets directly.

48cf commented 6 years ago

I want to achieve the behavior of JavaScript's buffer objects.

On Tue, Jun 5, 2018 at 9:40 PM, Igor Mroz notifications@github.com wrote:

Can't you just create string from bytes (with constructor)? If you want to send raw bytes, then you should probably use sockets directly.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ipkn/crow/issues/306#issuecomment-394834707, or mute the thread https://github.com/notifications/unsubscribe-auth/AfVEkZYr6rUnyWgd18-HEQT6yHEdPniYks5t5t6QgaJpZM4UbKcI .

mrozigor commented 6 years ago

Can you show example?

48cf commented 6 years ago

Uhh... The best thing I can do is log a Buffer object to console.

console.log(new Buffer("test")); results in: <Buffer 74 65 73 74>

I think Python's buffer = bytes() is a good representation too.

mrozigor commented 6 years ago

So you want to send bytes to socket. I think that you need raw socket to do this (https://www.geeksforgeeks.org/socket-programming-cc/). Or maybe send bytes as hex representation (https://stackoverflow.com/questions/10599068/how-do-i-print-bytes-as-hexadecimal) in HTTP response body and then reinterpret them on other side.

48cf commented 6 years ago

I can't use socket. I need to send response in that format to a HTTP server. Maybe i should just make a char buffer and "write bytes" to it? And In the end construct a string from it and send it back.

mrozigor commented 6 years ago

But HTTP protocol is a text protocol (as name states). If you need to send bytes to HTTP server then you can send theirs hex representation and then parse it back. Or send text representation of bytes (with std::string constructor).

48cf commented 6 years ago

Yeah, that's what I mean! Send their hexadecimal representation to the server! Is this possible with crow?

mrozigor commented 6 years ago

As I replied earlier - you have to convert bytes into hex string and then write it to response. I put link on how to perform this conversion in one of the earlier posts.

On 13 June 2018 14:28:50 CEST, czapek notifications@github.com wrote:

Yeah, that's what I mean! Send their hexadecimal representation to the server! Is this possible with crow?

-- You are receiving this because you commented. Reply to this email directly or view it on GitHub: https://github.com/ipkn/crow/issues/306#issuecomment-396919866

pierobot commented 6 years ago

Just use std::string. It can hold literally anything you want so long as you use it correctly.

If the other side of the connection expects raw data as the body of the response then it can be something as simple as

CROW_ROUTE(...)([...](...) -> crow::response
{
    char buffer[4] = { '\x76', '\x65', '\x73', '\x74'};
    auto response = crow::response(std::string(&buffer[0], sizeof(buffer)));
    // add whatever headers you need
    //response.set_header("Content-Type", "application/octet-stream");

    return response;
});

A somewhat more concrete example if you're trying to send a file

std::string const get_file_contents(...)
{
    std::string file_contents;
    file_contents.resize(file_size);

    // ... read to &file_contents[0]

    return file_contents;
}

CROW_ROUTE(...)([...](...) -> crow::response
{
    auto response = crow::response(get_file_contents(...));
    // add whatever headers you need
    //response.set_header("Content-Type", "application/octet-stream");

    return response;
});