Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Solved] HTTP File download problem  (Read 5460 times)

0 Members and 1 Guest are viewing this topic.

Amadeus

  • Newbie
  • *
  • Posts: 5
    • View Profile
[Solved] HTTP File download problem
« on: September 18, 2010, 05:44:25 pm »
Hi

EDIT: using SFML 1.6

Im strugling a little problem when using HTTP class to download files.

Currently planing to do auto updater to my little project.

I have virtual test server where I run apache www server which hosts files what clients need to update if there is changes in client side. This way I do not need to write update server from scratch.

Client are on W7 and server is on Ubuntu 10.04

But to the problem. When download a file it will have some extra bytes in it and wont work as it should.

For example dat.exe 274944 on server side (just for dummy test exe)
after download file size is dat.exe 277811

Because this file is corrupted and cannot be use as it should.

Made some tests and it seems that amount of extra bytes changes time to time.

There must be something that I dont see ;)

code snip from method which is called for update (Shortest 'working' code) stripped loggins and other not relevant lines.
Code: [Select]

bool cUpdater::bDownloadChangedFiles(const std::string _sFilename, sf::RenderWindow &_APP) {
sf::Http Http;

Http.SetHost("192.168.1.34");

std::string sURI;
std::string sDestination;
        sURI = "/" + _sFilename ;  // filename which need to update

sf::Http::Request Request;
Request.SetMethod(sf::Http::Request::Get);
Request.SetURI(sURI);

        sf::Http::Response Page = Http.SendRequest(Request);

int iFileSize = 0;

iFileSize = Page.GetBody().size() ;

// IS this right way to handle GetBody for 'binary' files
std::string sFileContainer = Page.GetBody();

// just for test
        const char * cs = sFileContainer.c_str ();

// quick and dirty save destination
sDestination = "O:\\xth\\data\\"+_sFilename ;

// Open file to write
        std::ofstream toFile(sDestination, std::ios::out, std::ios::binary);
// Using put() to write to file
for (int i = 0; i<iFileSize; i++) {
            toFile.put(cs[i]);
}
// Tested also write, but same problem
        // toFile.write((char*)&sFileContainer, iFileSize);
toFile.close();
        return true;
}



Cheers
Amadeus

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
[Solved] HTTP File download problem
« Reply #1 on: September 20, 2010, 09:13:05 am »
- Create a tiny text file (25 bytes ?)
- Download it
- See the difference in a text editor

I think you maybe get the HTTP header in the beginning or something like that ;)

EDIT : The GetBody function must bewithout the header, but do this little test in order to see what you get.
Mindiell
----

Amadeus

  • Newbie
  • *
  • Posts: 5
    • View Profile
[Solved] HTTP File download problem
« Reply #2 on: September 20, 2010, 06:25:13 pm »
Hello

I did a test. Made text file that is 32 bytes and after download it was 48. Lines was not changed.

Just got hunch that enter's is converted to \r\n that would explain 16 bytes grow in file.

Original file was created in linux box before download test.

A bit annoying problem that plain binary files are affected by this too

test text file was
Code: [Select]

1
2
3
4
5
6
7
8
9
0
a
b
c
d
e
f

Silvah

  • Guest
Re: HTTP File download problem
« Reply #3 on: September 20, 2010, 10:13:45 pm »
Quote from: "Amadeus"
Code: [Select]

std::ofstream toFile(sDestination, std::ios::out, std::ios::binary);
Did you mean:
Code: [Select]
std::ofstream toFile(sDestination, std::ios::out | std::ios::binary);?

Amadeus

  • Newbie
  • *
  • Posts: 5
    • View Profile
[Solved] HTTP File download problem
« Reply #4 on: September 21, 2010, 07:41:29 am »
Hello

Thank you Silvah that was the problem. Sometimes it is very hard to find own mistakes :)


Cheers
Amadeus

 

anything