LoginSignup
3
2

More than 5 years have passed since last update.

HEADメソッドに対応してないところにwget --spiderを実行したときの挙動

Posted at

wgetのスパイダーモードで200 OKが返るか判定 というのをやってみたら、知り合いから、「スパイダーモードはHEADメソッドだから対応していないサーバではうまくいかないのでは」と下記の記事を教えてもらいました。

気になったので、下記のようなplay javaのコードで、HEADメソッドのときに、色んなステータスコードを返させて、wget --spiderの挙動をみてみました。

import play.*;
import play.mvc.*;
import views.html.*;

public class Application extends Controller {

    public Result index() {
        Logger.info("http method:" + request().method());
        if (request().method().equals("HEAD")) {
            //ここで返却するステースコードを変える
            return status(Http.Status.INTERNAL_SERVER_ERROR); 
        }
        return ok(index.render("Your new application is ready."));
    }

}

400系は恐らく、broken link!と返される模様。

405 Method not allowed

wget --spider  http://localhost:9000
Spider mode enabled. Check if remote file exists.
--2015-10-30 23:15:26--  http://localhost:9000/
Resolving localhost... ::1, 127.0.0.1, fe80::1
Connecting to localhost|::1|:9000... connected.
HTTP request sent, awaiting response... 405 Method Not Allowed
Remote file does not exist -- broken link!!!

400 Bad request

wget --spider  http://localhost:9000
Spider mode enabled. Check if remote file exists.
--2015-10-30 23:17:40--  http://localhost:9000/
Resolving localhost... ::1, 127.0.0.1, fe80::1
Connecting to localhost|::1|:9000... connected.
HTTP request sent, awaiting response... 400 Bad Request
Remote file does not exist -- broken link!!!

500 Internal Server Error

今回のAPIの仕様のせいかもしれないが、HEADでだめだったが、残ったコネクション使って、なぜかGETでリクエストし直してるようです。

wget --spider  http://localhost:9000
Spider mode enabled. Check if remote file exists.
--2015-10-30 23:20:52--  http://localhost:9000/
Resolving localhost... ::1, 127.0.0.1, fe80::1
Connecting to localhost|::1|:9000... connected.
HTTP request sent, awaiting response... 500 Internal Server Error
--2015-10-30 23:20:53--  (try: 2)  http://localhost:9000/
Reusing existing connection to [localhost]:9000.
HTTP request sent, awaiting response... 200 OK
Length: 6878 (6.7K) [text/html]
Saving to: 'index.html.7'

index.html.7                     100%[=========================================================>]   6.72K  --.-KB/s   in 0s     

2015-10-30 23:20:53 (262 MB/s) - 'index.html.7' saved [6878/6878]

503 Service Unavailable

これは400系と同じ結果。

wget --spider  http://localhost:9000
Spider mode enabled. Check if remote file exists.
--2015-10-30 23:20:24--  http://localhost:9000/
Resolving localhost... ::1, 127.0.0.1, fe80::1
Connecting to localhost|::1|:9000... connected.
HTTP request sent, awaiting response... 503 Service Unavailable
Remote file does not exist -- broken link!!!

504 Gateway timeout

HEADメソッドでリトライし続けました。

wget --spider  http://localhost:9000
Spider mode enabled. Check if remote file exists.
--2015-10-30 23:19:15--  http://localhost:9000/
Resolving localhost... ::1, 127.0.0.1, fe80::1
Connecting to localhost|::1|:9000... connected.
HTTP request sent, awaiting response... 504 Gateway Timeout
Retrying.

Spider mode enabled. Check if remote file exists.
--2015-10-30 23:19:17--  (try: 2)  http://localhost:9000/
Reusing existing connection to [localhost]:9000.
HTTP request sent, awaiting response... 504 Gateway Timeout
Retrying.

Spider mode enabled. Check if remote file exists.
--2015-10-30 23:19:19--  (try: 3)  http://localhost:9000/
Reusing existing connection to [localhost]:9000.
HTTP request sent, awaiting response... 504 Gateway Timeout
Retrying.

結論 --delete-after にしよう

GETメソッドでリクエストして、ダウンロードしたら自動で消すというオプションです。

wget --delete-after http://localhost:9000 2>&1 | grep '200 OK'

curl派なら、こちらを

curl -v  http://localhost:9000 2>&1 > /dev/null | grep '200 OK'

蛇足:「wget」のソースコードみればいいのでは?

そう思ったのですが、疲れたので断念しました...

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2