http

package module
v0.0.0-...-43da0c8 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2019 License: MIT Imports: 8 Imported by: 0

README

http

This project contains solutions for the first two assignments for the Data Communications & Computer Networks course.

The parent directory is a package exposing partial http/1.0 client and server implementation similar to net/http.

The ./httpc and ./httpfs directories contain a curl-like command line tool and a simple file server respectively.

httpc

$ go get -u github.com/g-harel/http/httpc
get
Executes a HTTP GET request for a given URL.

Usage:
    httpc get [-v] [-h key:value] [-o filename] URL

Flags:
   -v             Prints the detail of the response such as protocol, status, and headers.
   -h key:value   Associates headers to HTTP Request with the format 'key:value'.
   -o filename    Writes response body to specified file.
post
Executes a HTTP POST request for a given URL with inline data or from file.

Usage:
   httpc post [-v] [-h key:value] [-d inline-data] [-f file] [-o filename] URL

Flags:
   -v             Prints the detail of the response such as protocol, status, and headers.
   -h key:value   Associates headers to HTTP Request with the format 'key:value'.
   -d string      Associates an inline data to the body HTTP POST request.
   -f filename    Associates the content of a file to the body HTTP POST request.
   -o filename    Writes response body to specified file.

Either -d or -f can be used but not both.

httpfs

$ go get -u github.com/g-harel/http/httpfs
Starts a simple file server.

Usage:
   httpfs [-v] [-p port] [-d directory]

Flags:
   -v             Prints debugging messages.
   -p port        Specifies the port for the server to listen to (default "8080").
   -d directory   Specifies the file server's root directory (default ".").

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	FollowRedirect bool
	Logger         io.Writer
}

Client is used to configure and send http requests.

func (*Client) Send

func (c *Client) Send(req *Request) (*Response, error)

Send sends an http request.

type ErrorHandler

type ErrorHandler func(err error) *Response

ErrorHandler should produce a response from an error.

type Handler

type Handler func(req *Request) (*Response, error)

Handler function handles the server's requests.

type Headers

type Headers map[string]string

Headers type represents an http request's headers.

func (*Headers) Add

func (h *Headers) Add(name, value string) *Headers

Add adds a name/value combination to the Headers' data.

func (*Headers) AddRaw

func (h *Headers) AddRaw(lines ...string) *Headers

AddRaw parses the input string to extract and add name/value combinations. The format of the string should be "{name}:{value}". Everything before the first colon is the name and everything after is the value.

func (*Headers) Fprint

func (h *Headers) Fprint(w io.Writer) error

Fprint writes the headers to the given writer.

func (*Headers) Read

func (h *Headers) Read(name string) (string, bool)

Read reads the header value for the given name.

type Request

type Request struct {
	Version  string
	Method   string
	Hostname string
	Port     string
	Path     string
	Query    string
	Headers  *Headers
	Body     io.Reader
	// contains filtered or unexported fields
}

Request represents an HTTP request to be sent.

func ReadRequest

func ReadRequest(r io.Reader) (*Request, error)

ReadRequest parses and reads a request from r.

func (*Request) Close

func (r *Request) Close() error

Close closes the request's connection.

func (*Request) Fprint

func (r *Request) Fprint(w io.Writer) error

Fprint writes the formatted request to w.

func (*Request) URL

func (r *Request) URL(addr string) error

URL fills in the Hostname, Port and RequestURI fields in r from a url string.

type Response

type Response struct {
	Version    string
	Status     string
	StatusCode int
	Headers    *Headers
	Body       io.Reader
	// contains filtered or unexported fields
}

Response represents a received HTTP response.

func NewResponse

func NewResponse(code int, body ...string) *Response

NewResponse creates a response for the given status and message. If no strings are given to fill the body, the status message is used. Body strings are joined with newlines.

func ReadResponse

func ReadResponse(r io.Reader) (*Response, error)

ReadResponse parses and reads a response from r.

func (*Response) Close

func (r *Response) Close() error

Close closes the response's connection.

func (*Response) Fprint

func (r *Response) Fprint(w io.Writer) error

Fprint writes the formatted response to w.

type Server

type Server struct {

	// Errors that cannot be handled by errHandler are sent to this channel.
	ErrChan chan error
	// contains filtered or unexported fields
}

Server is used to respond to http requests.

func (*Server) Catch

func (s *Server) Catch(h ErrorHandler)

Catch configures the server to use the given ErrorHandler.

func (*Server) Listen

func (s *Server) Listen(port string) error

Listen listens for incoming requests on the requested port.

func (*Server) Use

func (s *Server) Use(h Handler)

Use configures the server to use the given Handler.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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