pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/httputil/httputil.go (about)

     1  // Package httputil provides methods for working with HTTP request/responses
     2  package httputil
     3  
     4  // ////////////////////////////////////////////////////////////////////////////////// //
     5  //                                                                                    //
     6  //                         Copyright (c) 2022 ESSENTIAL KAOS                          //
     7  //      Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0>     //
     8  //                                                                                    //
     9  // ////////////////////////////////////////////////////////////////////////////////// //
    10  
    11  import (
    12  	"net/http"
    13  	"strings"
    14  )
    15  
    16  // ////////////////////////////////////////////////////////////////////////////////// //
    17  
    18  // GetRequestAddr returns host and port info from request
    19  func GetRequestAddr(r *http.Request) (string, string) {
    20  	if r == nil || r.Host == "" || r.URL == nil {
    21  		return "", ""
    22  	}
    23  
    24  	hostSlice := strings.Split(r.Host, ":")
    25  
    26  	switch len(hostSlice) {
    27  	case 2:
    28  		return hostSlice[0], hostSlice[1]
    29  	default:
    30  		return hostSlice[0], GetPortByScheme(r.URL.Scheme)
    31  	}
    32  }
    33  
    34  // GetRequestHost returns host from request struct
    35  func GetRequestHost(r *http.Request) string {
    36  	host, _ := GetRequestAddr(r)
    37  	return host
    38  }
    39  
    40  // GetRequestPort returns port from request struct
    41  func GetRequestPort(r *http.Request) string {
    42  	_, port := GetRequestAddr(r)
    43  	return port
    44  }
    45  
    46  // GetRemoteAddr returns network address that sent the request
    47  func GetRemoteAddr(r *http.Request) (string, string) {
    48  	addr := r.RemoteAddr
    49  
    50  	if addr == "" || !strings.Contains(addr, ":") {
    51  		return "", ""
    52  	}
    53  
    54  	addrSlice := strings.Split(addr, ":")
    55  
    56  	return addrSlice[0], addrSlice[1]
    57  }
    58  
    59  // GetRemoteHost returns host that sent the request
    60  func GetRemoteHost(r *http.Request) string {
    61  	host, _ := GetRemoteAddr(r)
    62  	return host
    63  }
    64  
    65  // GetRemotePort returns port of the host that sent the request
    66  func GetRemotePort(r *http.Request) string {
    67  	_, port := GetRemoteAddr(r)
    68  	return port
    69  }
    70  
    71  // GetPortByScheme returns port for supported scheme
    72  func GetPortByScheme(s string) string {
    73  	switch strings.ToLower(s) {
    74  	case "http":
    75  		return "80"
    76  	case "https":
    77  		return "443"
    78  	case "ftp":
    79  		return "21"
    80  	}
    81  
    82  	return ""
    83  }
    84  
    85  // GetDescByCode returns response code description
    86  func GetDescByCode(code int) string {
    87  	switch code {
    88  	case 100:
    89  		return "Continue"
    90  	case 101:
    91  		return "Switching Protocols"
    92  	case 200:
    93  		return "OK"
    94  	case 201:
    95  		return "Created"
    96  	case 202:
    97  		return "Accepted"
    98  	case 203:
    99  		return "Non Authoritative Info"
   100  	case 204:
   101  		return "No Content"
   102  	case 205:
   103  		return "Reset Content"
   104  	case 206:
   105  		return "Partial Content"
   106  	case 300:
   107  		return "Multiple Choices"
   108  	case 301:
   109  		return "Moved Permanently "
   110  	case 302:
   111  		return "Found"
   112  	case 303:
   113  		return "See Other"
   114  	case 304:
   115  		return "Not Modified"
   116  	case 305:
   117  		return "Use Proxy"
   118  	case 307:
   119  		return "Temporary Redirect"
   120  	case 400:
   121  		return "Bad Request"
   122  	case 401:
   123  		return "Unauthorized"
   124  	case 402:
   125  		return "Payment Required"
   126  	case 403:
   127  		return "Forbidden"
   128  	case 404:
   129  		return "Not Found"
   130  	case 405:
   131  		return "Method Not Allowed"
   132  	case 406:
   133  		return "Not Acceptable"
   134  	case 407:
   135  		return "Proxy Auth Required"
   136  	case 408:
   137  		return "Request Timeout"
   138  	case 409:
   139  		return "Conflict"
   140  	case 410:
   141  		return "Gone"
   142  	case 411:
   143  		return "Length Required"
   144  	case 412:
   145  		return "Precondition Failed"
   146  	case 413:
   147  		return "Request Entity Too Large"
   148  	case 414:
   149  		return "Request URI TooLong"
   150  	case 415:
   151  		return "Unsupported Media Type"
   152  	case 416:
   153  		return "Requested Range Not Satisfiable"
   154  	case 417:
   155  		return "Expectation Failed"
   156  	case 418:
   157  		return "Teapot"
   158  	case 500:
   159  		return "Internal Server Error"
   160  	case 501:
   161  		return "Not Implemented"
   162  	case 502:
   163  		return "Bad Gateway"
   164  	case 503:
   165  		return "Service Unavailable"
   166  	case 504:
   167  		return "Gateway Timeout"
   168  	case 505:
   169  		return "HTTP Version Not Supported"
   170  	default:
   171  		return "Unknown"
   172  	}
   173  }
   174  
   175  // IsURL check if given value is valid URL or not
   176  func IsURL(url string) bool {
   177  	switch {
   178  	case len(url) < 10:
   179  		return false
   180  	case url[0:7] == "http://":
   181  		return true
   182  	case url[0:8] == "https://":
   183  		return true
   184  	case url[0:6] == "ftp://":
   185  		return true
   186  	}
   187  
   188  	return false
   189  }