Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sending file to crow backend #210

Closed
mihdim777 opened this issue Aug 24, 2021 · 13 comments
Closed

Sending file to crow backend #210

mihdim777 opened this issue Aug 24, 2021 · 13 comments
Labels
bug Something isn't working

Comments

@mihdim777
Copy link

mihdim777 commented Aug 24, 2021

Hello!

I am trying to send a file (any extension) to a backend crow server.

I am using this piece of code and somehow files don't have the same SHA hash.
In Visual Studio I have set to C++17 and Character Set to Use Multi-Byte Character Set.

CROW_ROUTE(app, "/file").methods("POST"_method) ([](const crow::request& req){
		crow::multipart::message msg(req);
		
		auto filepath = msg.parts[0].body;
		auto file = msg.parts[1].body;

		auto file_handler = std::ofstream(filepath);
		file_handler << file;

		file_handler.close();

		return crow::response(200, "Ok");
		});

What am I doing wrong?

image

Thank you!

@The-EDev
Copy link
Member

The-EDev commented Aug 24, 2021

I ran your code on my machine (Linux), everything worked fine..
image

I'm using GCC 11.1.0

@The-EDev The-EDev added the bug Something isn't working label Aug 24, 2021
@The-EDev
Copy link
Member

Do you mind telling me how you got Crow and which version you're using? Thanks.

@epajarre
Copy link
Contributor

Does the resulting file have the same size?

I sort of suspect Windows binary/non binary file issue. You could test opening your output file with binary mode flag.

@The-EDev
Copy link
Member

The-EDev commented Aug 24, 2021

Sorry for the late response, I was spinning up a windows VM to test, there is definitely something weird going on, Crow seems to be duplicating newlines in the written file.. I'll look into it further

Well it seems to only happen on windows, same data with Linux produces fine results

@The-EDev
Copy link
Member

@epajarre I don't have much experience with windows, the example shows a text file and @mihdim777 is saying the SHA checksum isn't matching..

@The-EDev
Copy link
Member

Okay I ran VS debugger, it's not Crow's fault, windows uses CRLF (carriage return, line feed) for a newline, somehow when you're doing file_handler << file; it's duplicating every CR character

Notice how every 0d in this sequence is being repeated,
0d = CR
0a = LF
image

@epajarre
Copy link
Contributor

So using something like this:

auto file_handler = std::ofstream(filepath,std::ofstream::binary);

should fix the issue.

@mihdim777
Copy link
Author

mihdim777 commented Aug 25, 2021

Hello!

Sorry for late response and thank you guys for helping!

@The-EDev Thank you very much for investigation. Indeed it was from Windows machine with that CRLF.

@epajarre yes, by specifying binary ofstream did the trick and now they have the same SHA and files are reopened perfectly.

I'm using the code version from this repo.

Do you think it is a good idea to implement in crow a function to handle file opening, writing and closing? Should I create a PR?

@mihdim777
Copy link
Author

mihdim777 commented Aug 25, 2021

Hello!

Also I found this after redownloading this the latest version:

Here is exception threw on this code:

`
#define CROW_MAIN
#include "crow.h"

int main()
{
crow::SimpleApp app;

  CROW_ROUTE(app, "/")([](){
      return "Hello world";
  });

  app.port(8080).multithreaded().run();

}
`

image

And here is the call stack from routing.h

image

@mihdim777
Copy link
Author

mihdim777 commented Aug 25, 2021

@The-EDev , @epajarre

By adding check for blueprints, route is working again, I am getting response from the server.

image

@The-EDev
Copy link
Member

Also I found this after redownloading this the latest version

Thanks for taking note of this problem, it's been fixed in #208 which I just merged

@The-EDev
Copy link
Member

Do you think it is a good idea to implement in crow a function to handle file opening, writing and closing?

I'm not sure, std::ofstream is already simple enough in my opinion, doesn't make much sense to wrap it

@The-EDev
Copy link
Member

Thanks for your help @epajarre, I had completely forgotten about std::ofstream::binary.

I'll close this issue since the problem is fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants