rawhttp

package module
v0.0.0-...-e98e9bb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 11, 2022 License: MIT Imports: 12 Imported by: 22

README

rawhttp

Build Status Documentation

rawhttp is a Go package for making HTTP requests. It intends to fill a niche that https://golang.org/pkg/net/http/ does not cover: having complete control over the requests being sent to the server.

rawhttp purposefully does as little validation as possible, and you can override just about anything about the request; even the line endings.

Warning: This is a work in progress. The API isn't fixed yet.

Full documentation can be found on GoDoc.

Example

req, err := rawhttp.FromURL("POST", "https://httpbin.org")
if err != nil {
	log.Fatal(err)
}

// automatically set the host header
req.AutoSetHost()

req.Method = "PUT"
req.Hostname = "httpbin.org"
req.Port = "443"
req.Path = "/anything"
req.Query = "one=1&two=2"
req.Fragment = "anchor"
req.Proto = "HTTP/1.1"
req.EOL = "\r\n"

req.AddHeader("Content-Type: application/x-www-form-urlencoded")

req.Body = "username=AzureDiamond&password=hunter2"

// automatically set the Content-Length header
req.AutoSetContentLength()

fmt.Printf("%s\n\n", req.String())

resp, err := rawhttp.Do(req)
if err != nil {
	log.Fatal(err)
}

fmt.Printf("< %s\n", resp.StatusLine())
for _, h := range resp.Headers() {
	fmt.Printf("< %s\n", h)
}

fmt.Printf("\n%s\n", resp.Body())
PUT /anything?one=1&two=2#anchor HTTP/1.1
Host: httpbin.org
Content-Type: application/x-www-form-urlencoded
Content-Length: 38

username=AzureDiamond&password=hunter2

< HTTP/1.1 200 OK
< Connection: keep-alive
< Server: meinheld/0.6.1
< Date: Sat, 02 Sep 2017 13:22:06 GMT
< Content-Type: application/json
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Credentials: true
< X-Powered-By: Flask
< X-Processed-Time: 0.000869989395142
< Content-Length: 443
< Via: 1.1 vegur

{
  "args": {
    "one": "1",
    "two": "2"
  },
  "data": "",
  "files": {},
  "form": {
    "password": "hunter2",
    "username": "AzureDiamond"
  },
  "headers": {
    "Connection": "close",
    "Content-Length": "38",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "httpbin.org"
  },
  "json": null,
  "method": "PUT",
  "origin": "123.123.123.123",
  "url": "https://httpbin.org/anything?one=1&two=2"
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RawRequest

type RawRequest struct {
	// TLS should be true if TLS should be used
	TLS bool

	// Hostname is the name of the host to connect to. E.g: localhost
	Hostname string

	// Port is the port to connect to. E.g.: 80
	Port string

	// Request is the actual message to send to the server. E.g:
	//   GET / HTTP/1.1\r\nHost:...
	Request string

	// Timeout for the request
	Timeout time.Duration
}

RawRequest is the most basic implementation of Requester. You should probably only use it if you're doing something *really* weird

func (RawRequest) GetTimeout

func (r RawRequest) GetTimeout() time.Duration

GetTimeout returns the timeout for the request

func (RawRequest) Host

func (r RawRequest) Host() string

Host returns the hostname:port pair

func (RawRequest) IsTLS

func (r RawRequest) IsTLS() bool

IsTLS returns true if the connection should use TLS

func (RawRequest) String

func (r RawRequest) String() string

String returns the message to send to the server

type Request

type Request struct {
	// TLS should be true if TLS should be used
	TLS bool

	// Method is the HTTP verb. E.g. GET
	Method string

	// Scheme is the protocol scheme. E.g. https
	Scheme string

	// Hostname is the hostname to connect to. E.g. localhost
	Hostname string

	// Port is the port to connect to. E.g. 80
	Port string

	// Path is the path to request. E.g. /security.txt
	Path string

	// Query is the query string of the path. E.g. q=searchterm&page=3
	Query string

	// Fragment is the bit after the '#'. E.g. pagesection
	Fragment string

	// Proto is the protocol specifier in the first line of the request.
	// E.g. HTTP/1.1
	Proto string

	// Headers is a slice of headers to send. E.g:
	//   []string{"Host: localhost", "Accept: text/plain"}
	Headers []string

	// Body is the 'POST' data to send. E.g:
	//   username=AzureDiamond&password=hunter2
	Body string

	// EOL is the string that should be used for line endings. E.g. \r\n
	EOL string

	// Deadline
	Timeout time.Duration
}

Request is the main implementation of Requester. It gives you fine-grained control over just about everything to do with the request, but with the posibility of sane defaults.

func FromURL

func FromURL(method, rawurl string) (*Request, error)

FromURL returns a *Request for a given method and URL and any error that occured during parsing the URL. Sane defaults are set for all of *Request's fields.

func (*Request) AddHeader

func (r *Request) AddHeader(h string)

AddHeader adds a header to the *Request

func (*Request) AutoSetContentLength

func (r *Request) AutoSetContentLength()

AutoSetContentLength adds a Content-Length header to the request with the length of Request.Body as the value

func (*Request) AutoSetHost

func (r *Request) AutoSetHost()

AutoSetHost adds a Host header to the request using the value of Request.Hostname

func (Request) GetTimeout

func (r Request) GetTimeout() time.Duration

GetTimeout returns the timeout for a request

func (Request) Header

func (r Request) Header(search string) string

Header finds and returns the value of a header on the request. An empty string is returned if no match is found.

func (Request) Host

func (r Request) Host() string

Host returns the hostname:port pair to connect to

func (Request) IsTLS

func (r Request) IsTLS() bool

IsTLS returns true if TLS should be used

func (Request) RequestLine

func (r Request) RequestLine() string

RequestLine returns the request line. E.g. GET / HTTP/1.1

func (Request) String

func (r Request) String() string

String returns a plain-text version of the request to be sent to the server

func (Request) URL

func (r Request) URL() string

URL forms and returns a complete URL for the request

type Requester

type Requester interface {
	// IsTLS should return true if the connection should be made using TLS
	IsTLS() bool

	// Host should return a hostname:port pair
	Host() string

	// String should return the request as a string E.g:
	//   GET / HTTP/1.1\r\nHost:...
	String() string

	// GetTimeout returns the timeout for a request
	GetTimeout() time.Duration
}

A Requester defines the bare minimum set of methods needed to make an HTTP request.

type Response

type Response struct {
	// contains filtered or unexported fields
}

A Response wraps the HTTP response from the server

func Do

func Do(req Requester) (*Response, error)

Do performs the HTTP request for the given Requester and returns a *Response and any error that occured

func (Response) Body

func (r Response) Body() []byte

Body returns the response body

func (Response) Header

func (r Response) Header(search string) string

Header finds and returns the value of a header on the response. An empty string is returned if no match is found.

func (Response) Headers

func (r Response) Headers() []string

Headers returns the response headers

func (Response) ParseLocation

func (r Response) ParseLocation(req *Request) string

ParseLocation parses the Location header of a response, using the initial request for context on relative URLs

func (Response) StatusCode

func (r Response) StatusCode() string

StatusCode returns the HTTP status code as a string; e.g. 200

func (Response) StatusLine

func (r Response) StatusLine() string

StatusLine returns the HTTP status line from the response

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL