github.com/puellanivis/breton@v0.2.16/lib/files/httpfiles/http.go (about)

     1  // Package httpfiles implements the "http:" and "https:" URL schemes.
     2  package httpfiles
     3  
     4  import (
     5  	"context"
     6  	"errors"
     7  	"net/http"
     8  	"net/url"
     9  	"os"
    10  
    11  	"github.com/puellanivis/breton/lib/files"
    12  )
    13  
    14  type handler struct{}
    15  
    16  var schemes = map[string]string{
    17  	"http":  "80",
    18  	"https": "443",
    19  }
    20  
    21  func init() {
    22  	var schemeList []string
    23  
    24  	for scheme := range schemes {
    25  		schemeList = append(schemeList, scheme)
    26  	}
    27  
    28  	files.RegisterScheme(&handler{}, schemeList...)
    29  }
    30  
    31  func elideDefaultPort(uri *url.URL) *url.URL {
    32  	port := uri.Port()
    33  
    34  	/* elide default ports  */
    35  	if defport, ok := schemes[uri.Scheme]; ok && defport == port {
    36  		newuri := *uri
    37  		newuri.Host = uri.Hostname()
    38  		return &newuri
    39  	}
    40  
    41  	return uri
    42  }
    43  
    44  func getErr(resp *http.Response) error {
    45  	switch resp.StatusCode {
    46  	case http.StatusOK, http.StatusNoContent:
    47  		return nil
    48  	case http.StatusUnauthorized, http.StatusForbidden:
    49  		return os.ErrPermission
    50  	case http.StatusNotFound:
    51  		return os.ErrNotExist
    52  	}
    53  
    54  	return errors.New(resp.Status)
    55  }
    56  
    57  func (h *handler) List(ctx context.Context, uri *url.URL) ([]os.FileInfo, error) {
    58  	return nil, files.PathError("readdir", uri.String(), os.ErrInvalid)
    59  }