Help with network.download - when no files are present on server, it creates one anyway after the download attempt. What is going on?

So I have been working with network.download() for the first time and was having a good deal of success until I removed all my files from my server to test what would happen.   Here is sample code:

[lua]if require(“socket”).connect(“www.scottadelman.com”, 80) == nil then
print(“No INTERNET connection – Use Current Database”)
else
print(“getting for the first time”)

local function networkListener( event )
if ( event.isError ) then
print( “Network error!”)
elseif ( event.phase == “ended” ) then
print(event.status)
if event.status >= 200 and event.status < 300 then – file exists. call the function to download it.
network.download( “http://www.scottadelman.com/stored/ztp/banners.db”, “GET”, downloadResponse , “banners.db”, system.CachesDirectory )
else – file does not exist. call the function to get it created and then download it.
print(“FILE NOT ON SERVER”)
end

end
end

network.request( “http://www.scottadelman.com/stored/ztp/banners.db”, “HEAD”, networkListener )

end[/lua]

The print(event.status) returns with “200” even though the file does not exist on the server.  It then puts this in the banners.db file and saves it causes lots of errors:

[lua]

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=“http://www.w3.org/1999/xhtml” xml:lang=“en”>
<head>
    <title>404 - Page Not Found</title>
    <meta http-equiv=“content-type” content=“text/html; charset=UTF-8” />
    <meta name=“robots” content=“noarchive” />
    <link rel=“shortcut icon” href=“http://www3.dragndropbuilder.com/developer/none.ico” />

    <style type=“text/css”>
    body {
        background: url(http://www3.dragndropbuilder.com/images/404background.jpg);
        font-family: Helvetica, arial, sans-serif;
        color: #ccc;
    }
    .alert-container {
        background: url(http://www3.dragndropbuilder.com/images/404_textbox.png);
        width: 918px;
        height: 142px;
        margin: 82px auto 0px;
    }
    .alert-inner {
        padding: 24px 0px 0px 209px;
    }
    .alert-heading {
        font-size: 50px;
        font-weight: bold;
        line-height: 50px;
    }
    .alert-subheading {
        margin-top: 8px;
        font-size: 28px;
        line-height: 28px;
    }
    .redirect {
        width: 918px;
        margin: 24px auto 0px;
        font-size: 14px;
        line-height: 14px;
        text-align: center;
    }
    .redirect a {
        color: #ffb300;
        text-decoration: none;
    }
    </style>
</head>
<body>
    <div class=“alert-container”>
        <div class=“alert-inner”>
            <div class=“alert-heading”>404 - Page Not Found</div>
            <div class=“alert-subheading”>Sorry, the page you are looking for does not exist</div>
        </div>
    </div>
    <div class=“redirect”>Please check the URL.  Otherwise, <a href=’/’>click here</a> to be redirected to the homepage.</div>
</body>
</html>
 

[/lua]

Why is this happening and what can I do?

Thanks,
Scott

Your web server is configured strangely.  Usually when one requests a file that doesn’t exist one gets a HTTP 404 response but your server is returning a 200 (HTTP OK) which makes the system think all is well and store the error page it received as if it was a valid file (which it is).

If you grab a non-existent file from the command line you can see exactly what the server is returning:

% curl -I 'http://www.scottadelman.com/stored/ztp/this-file-does-not-exist.txt' HTTP/1.1 200 OK Date: Fri, 30 Aug 2013 21:57:41 GMT Server: Apache Content-Type: text/html

Where it says:

HTTP/1.1 200 OK

it should say (something like):

HTTP/1.1 404 Not found

So you need to look at the configuration of your web server or talk to your hosting company.

Thanks for the help!  I found I had a custom 404.php file that was causing the problem.  Deleted it and am now getting the appropriate values.

Your web server is configured strangely.  Usually when one requests a file that doesn’t exist one gets a HTTP 404 response but your server is returning a 200 (HTTP OK) which makes the system think all is well and store the error page it received as if it was a valid file (which it is).

If you grab a non-existent file from the command line you can see exactly what the server is returning:

% curl -I 'http://www.scottadelman.com/stored/ztp/this-file-does-not-exist.txt' HTTP/1.1 200 OK Date: Fri, 30 Aug 2013 21:57:41 GMT Server: Apache Content-Type: text/html

Where it says:

HTTP/1.1 200 OK

it should say (something like):

HTTP/1.1 404 Not found

So you need to look at the configuration of your web server or talk to your hosting company.

Thanks for the help!  I found I had a custom 404.php file that was causing the problem.  Deleted it and am now getting the appropriate values.