uWebSockets returning a binary buffer

342 Views Asked by At

I'm using uWebSockets C++ trying to return a binary buffe. When I do res->end(...) it sends and empty response, here's my code:

.get("/binaryfile", [](auto *res, auto *req) {                                                                          
    const char * fname = "../server/test.png";                                                                          
    FILE* filp = fopen(fname, "rb" );                                                                                   
    if(!filp) {                                                                                                         
      res->end("File not found!");                                                                                      
    } else {                                                                                                            
      std::cout << "File found" << std::endl;                                                                           
      char * buffer = new char[BUFFERSIZE];                                                                             
      std::string result;                                                                                               
      size_t bt = 0;                                                                                                    
      size_t total_size = 0;                                                                                            
      while ( (bt = fread(buffer, sizeof(char), BUFFERSIZE, filp)) > 0 ) {                                              
          result.append(buffer);                                                                                        
          total_size = total_size + bt;                                                                                 
      }                                                                                                                 
      // Done and close.                                                                                                
      fclose(filp);                                                                                                     
      std::string_view bd(result.c_str(), total_size);                                                                  
      res->writeHeader("Content-Type", "image/png");                                                                    
      std::cout << bd <<std::endl;                                                                                      
      res->end(bd);                                                                                                     
    }        

When I check with cURL I get this:

> GET /binaryfile HTTP/1.1
> Host: 192.168.0.104:3000
> User-Agent: curl/7.58.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Content-Type: image/png
< uWebSockets: 18
< Content-Length: 3097
< 
Warning: Binary output can mess up your terminal. Use "--output -" to tell 
Warning: curl to output it to your terminal anyway, or consider "--output 
Warning: <FILE>" to save to a file.
* Failed writing body (0 != 1365)
* stopped the pause stream!
* Closing connection 0

As you can see, just before res->end(bd) there's an std::cout << bd << std::endl; that's writing the contents to stdout without issues. So, why the content is not sent as a response?.

0

There are 0 best solutions below