YAMAGUCHI::weblog

噛み付き地蔵に憧れて、この神の世界にやってきました。マドンナみたいな男の子、コッペです。

httpstatusコマンドで、HTTPのステータスコードをすばやくしらべる!

はじめに

こんにちは、Go界の温水洋一です。

一般的な Web Programmer ならば、HTTP Status code はすべて暗記していると聞きました。

しかし、僕は初心者なので、なかなか覚えきれていないので、HTTPのステータスコードをさがすのに便利なツールを用意しました。 httpstatus-go です。インストール方法は go build かなんかで以下をビルドしてください。

package main

import (
    "fmt"
    "os"
    "regexp"
    "strings"
)

type HTTPStatus struct {
    Code    string
    Message string
}

var HTTPStatusList = []HTTPStatus{
    HTTPStatus{"100", "Continue"},
    HTTPStatus{"101", "Switching Protocols"},
    HTTPStatus{"102", "Processing"},
    HTTPStatus{"200", "OK"},
    HTTPStatus{"201", "Created"},
    HTTPStatus{"202", "Accepted"},
    HTTPStatus{"203", "Non-Authoritative Information"},
    HTTPStatus{"204", "No Content"},
    HTTPStatus{"205", "Reset Content"},
    HTTPStatus{"206", "Partial Content"},
    HTTPStatus{"207", "Multi-Status"},
    HTTPStatus{"208", "Already Reported"},
    HTTPStatus{"300", "Multiple Choices"},
    HTTPStatus{"301", "Moved Permanently"},
    HTTPStatus{"302", "Found"},
    HTTPStatus{"303", "See Other"},
    HTTPStatus{"304", "Not Modified"},
    HTTPStatus{"305", "Use Proxy"},
    HTTPStatus{"307", "Temporary Redirect"},
    HTTPStatus{"400", "Bad Request"},
    HTTPStatus{"401", "Unauthorized"},
    HTTPStatus{"402", "Payment Required"},
    HTTPStatus{"403", "Forbidden"},
    HTTPStatus{"404", "Not Found"},
    HTTPStatus{"405", "Method Not Allowed"},
    HTTPStatus{"406", "Not Acceptable"},
    HTTPStatus{"407", "Proxy Authentication Required"},
    HTTPStatus{"408", "Request Timeout"},
    HTTPStatus{"409", "Conflict"},
    HTTPStatus{"410", "Gone"},
    HTTPStatus{"411", "Length Required"},
    HTTPStatus{"412", "Precondition Failed"},
    HTTPStatus{"413", "Request Entity Too Large"},
    HTTPStatus{"414", "Request-URI Too Large"},
    HTTPStatus{"415", "Unsupported Media Type"},
    HTTPStatus{"416", "Request Range Not Satisfiable"},
    HTTPStatus{"417", "Expectation Failed"},
    HTTPStatus{"418", "I'm a teapot"},
    HTTPStatus{"422", "Unprocessable Entity"},
    HTTPStatus{"423", "Locked"},
    HTTPStatus{"424", "Failed Dependency"},
    HTTPStatus{"425", "No code"},
    HTTPStatus{"426", "Upgrade Required"},
    HTTPStatus{"428", "Precondition Required"},
    HTTPStatus{"429", "Too Many Requests"},
    HTTPStatus{"431", "Request Header Fields Too Large"},
    HTTPStatus{"449", "Retry with"},
    HTTPStatus{"500", "Internal Server Error"},
    HTTPStatus{"501", "Not Implemented"},
    HTTPStatus{"502", "Bad Gateway"},
    HTTPStatus{"503", "Service Unavailable"},
    HTTPStatus{"504", "Gateway Timeout"},
    HTTPStatus{"505", "HTTP Version Not Supported"},
    HTTPStatus{"506", "Variant Also Negotiates"},
    HTTPStatus{"507", "Insufficient Storage"},
    HTTPStatus{"509", "Bandwidth Limit Exceeded"},
    HTTPStatus{"510", "Not Extended"},
    HTTPStatus{"511", "Network Authentication Required"},
}

func ShowStatus(validator func(s HTTPStatus) bool) {
    for _, s := range HTTPStatusList {
        if validator(s) {
            fmt.Println(s.Code, s.Message)
        }
    }
}

func main() {
    if len(os.Args) != 2 {
        ShowStatus(func(s HTTPStatus) bool { return true })
    } else {
        flag, err := regexp.MatchString(`^\d+$`, os.Args[1])
        if err != nil {
            panic(err)
        }
        if flag {
            ShowStatus(func(s HTTPStatus) bool {
                return strings.HasPrefix(s.Code, os.Args[1])
            })
        } else {
            ShowStatus(func(s HTTPStatus) bool {
                return regexp.MatchString(s.Message, os.Args[1])
            })
        }
    }
}

参考