gokhttp

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2020 License: MIT Imports: 29 Imported by: 0

README

Golang HTTP Client

Go Report Card License

Introduction

gOkHttp is HTTP Client written in Golang inspired by Java's (now Kotlin's) OkHTTP and also slightly inspired by Python's Requests.

This library has been written with handling cookies (in-memory, on-disk, encrypted on-disk), headers, parameters, post fields, post multipart bodies, post files and SSL pinning in mind.

  • Inspired by okhttp
    • SSL Pinning implementation and ease of using it
  • Inspired by requests
    • Response processing (eg: gokhttp.HttpResponse.Text() to return response body as a string)
    • Making requests with headers and parameters as maps (there where in Python it's dicts)
Warning

This library is probably not production ready yet, most of the core was coded in under 2 hours at 1 AM. Proceed with caution, this library was made to fit MY needs and therefore may be structured in a weird way or incomplete (feel free to fix it in a pull request).

Dependencies

Only external dependencies listed, much thanks to all of the repositories listed here!

  • cookies/jar.go
    • github.com/juju/go4/lock
  • response.go
    • github.com/anaskhan96/soup (for parsing HTML)
    • github.com/beevik/etree (for parsing XML)
    • github.com/buger/jsonparser (for parsing JSON)

Also thanks to github.com/juju/persistent-cookiejar as the coookie jar implementation is pretty much that code + AES encryption capabilities.

Installation

The package can be installed using "go get".

go get -u github.com/BRUHItsABunny/gOkHttp

Usage

There is a folder filled with examples here

Contributions

Feel free to fork this repository and open up pull requests.

Documentation

Index

Constants

View Source
const (
	StatusStart = iota
	StatusProgress
	StatusError
	StatusDone
	StatusMerging
)

Variables

View Source
var DefaultGOKHTTPOptions = &HttpClientOptions{
	JarOptions: &cookies.JarOptions{PublicSuffixList: publicsuffix.List, NoPersist: false, Filename: ".cookies", EncryptionPassword: ""},
	Transport: &http.Transport{
		TLSHandshakeTimeout: DefaultHTTPTimeout,
		DisableCompression:  false,
		DisableKeepAlives:   false,
	},
	RefererOptions: &RefererOptions{Update: true, Use: true},
	Timeout:        &DefaultHTTPTimeout,
}

proxyUrl, _ := url.Parse("http://127.0.0.1:8866")

View Source
var DefaultHTTPTimeout = time.Second * time.Duration(30)
View Source
var EmptyMap map[string]string

Functions

func Aggregate

func Aggregate(task *Task, wg *sync.WaitGroup)

func DownloadStatusString

func DownloadStatusString(status int) string

func ParseIndex

func ParseIndex(content []byte)

func ParsePlaylist

func ParsePlaylist(content []byte)

func UseDownloadReporter

func UseDownloadReporter(reporter DownloadReporter)

Types

type DefaultDownloadReporter

type DefaultDownloadReporter struct {
	Ticker     *time.Ticker
	Chan       chan *TrackerMessage
	InProgress map[int]Download
	Done       map[int]Download
	ShouldStop bool
	WaitTime   int
	WaitGroup  *sync.WaitGroup
}

func GetDefaultDownloadReporter

func GetDefaultDownloadReporter(channel chan *TrackerMessage, wg *sync.WaitGroup) *DefaultDownloadReporter

func (*DefaultDownloadReporter) OnDone

func (dr *DefaultDownloadReporter) OnDone(message *TrackerMessage) error

func (*DefaultDownloadReporter) OnError

func (dr *DefaultDownloadReporter) OnError(message *TrackerMessage) error

func (*DefaultDownloadReporter) OnMerging

func (dr *DefaultDownloadReporter) OnMerging(message *TrackerMessage) error

func (*DefaultDownloadReporter) OnProgress

func (dr *DefaultDownloadReporter) OnProgress(message *TrackerMessage) error

func (*DefaultDownloadReporter) OnStart

func (dr *DefaultDownloadReporter) OnStart(message *TrackerMessage) error

func (*DefaultDownloadReporter) OnTick

func (dr *DefaultDownloadReporter) OnTick() error

func (*DefaultDownloadReporter) Processor

func (dr *DefaultDownloadReporter) Processor() error

type Dialer

type Dialer func(ctx context.Context, network, addr string) (net.Conn, error)

func MakeDialer

func MakeDialer(pinner SSLPinner) Dialer

type Download

type Download struct {
	FileName string
	Size     int
	Progress int
	Delta    int
	Started  time.Time
	Finished time.Time
	Threaded bool
	Status   int
	// Threads
	Fragments map[string]Download
}

type DownloadReporter

type DownloadReporter interface {
	OnDone(message *TrackerMessage) error
	OnStart(message *TrackerMessage) error
	OnProgress(message *TrackerMessage) error
	OnError(message *TrackerMessage) error
	OnMerging(message *TrackerMessage) error
	OnTick() error
	Processor() error
}

type HttpClient

type HttpClient struct {
	Client         *http.Client
	RefererOptions RefererOptions
	Headers        map[string]string
	Context        *context.Context
	CancelF        context.CancelFunc
	ClientOptions  *HttpClientOptions
}

func GetHTTPClient

func GetHTTPClient(o *HttpClientOptions) HttpClient

func GetHTTPDownloadClient

func GetHTTPDownloadClient(o *HttpClientOptions) HttpClient

func (*HttpClient) CheckResource

func (c *HttpClient) CheckResource(task *Task, parameters url.Values, headers map[string]string) *Task

func (*HttpClient) CopyClient

func (c *HttpClient) CopyClient() HttpClient

func (*HttpClient) Do

func (c *HttpClient) Do(req *http.Request) (*HttpResponse, error)

func (*HttpClient) DownloadFile

func (c *HttpClient) DownloadFile(task *Task, parameters url.Values, headers map[string]string) error

func (*HttpClient) MakeGETRequest

func (c *HttpClient) MakeGETRequest(URL string, parameters url.Values, headers map[string]string) (*http.Request, error)

func (*HttpClient) MakeHEADRequest

func (c *HttpClient) MakeHEADRequest(URL string, parameters url.Values, headers map[string]string) (*http.Request, error)

func (*HttpClient) MakeMultiPartPOSTRequest

func (c *HttpClient) MakeMultiPartPOSTRequest(URL, contentType string, postBody io.Reader, headers map[string]string) (*http.Request, error)

func (*HttpClient) MakeMultiPartPOSTRequestWithParameters

func (c *HttpClient) MakeMultiPartPOSTRequestWithParameters(URL string, parameters url.Values, contentType string, postBody io.Reader, headers map[string]string) (*http.Request, error)

func (*HttpClient) MakePOSTRequest

func (c *HttpClient) MakePOSTRequest(URL string, postFields, headers map[string]string) (*http.Request, error)

func (*HttpClient) MakePOSTRequestWithParameters

func (c *HttpClient) MakePOSTRequestWithParameters(URL string, parameters url.Values, postFields, headers map[string]string) (*http.Request, error)

func (*HttpClient) MakePostFields

func (c *HttpClient) MakePostFields(postFields map[string]string) io.Reader

func (*HttpClient) MakePostMultiPart

func (c *HttpClient) MakePostMultiPart(postFields map[string]string, files map[string]io.Reader, fieldNames []string) (io.Reader, string, error)

func (*HttpClient) MakeRawPOSTRequest

func (c *HttpClient) MakeRawPOSTRequest(URL string, parameters url.Values, postBody io.Reader, headers map[string]string) (*http.Request, error)

func (*HttpClient) SaveCookies

func (c *HttpClient) SaveCookies() error

func (*HttpClient) SetCookiesWithStrings

func (c *HttpClient) SetCookiesWithStrings(cookieMap map[string][]*http.Cookie) error

func (*HttpClient) SetCookiesWithURLS

func (c *HttpClient) SetCookiesWithURLS(cookieMap map[*url.URL][]*http.Cookie) error

func (*HttpClient) SetProxy

func (c *HttpClient) SetProxy(proxyURLStr string) error

func (*HttpClient) StartThread

func (c *HttpClient) StartThread(task *Task, started chan bool, parameters url.Values, headers map[string]string) error

type HttpClientOptions

type HttpClientOptions struct {
	JarOptions        *cookies.JarOptions
	Transport         *http.Transport
	Timeout           *time.Duration
	SSLPinningOptions *SSLPinner
	RefererOptions    *RefererOptions
	RedirectPolicy    func(req *http.Request, via []*http.Request) error
	Headers           map[string]string
	Context           *context.Context
	CancelF           context.CancelFunc
}

type HttpJSONResponse

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

func (*HttpJSONResponse) ArrayEach

func (res *HttpJSONResponse) ArrayEach(cb func(value []byte, dataType jsonparser.ValueType, offset int, err error), keys ...string) (offset int, err error)

func (*HttpJSONResponse) Delete

func (res *HttpJSONResponse) Delete(keys ...string) []byte

func (*HttpJSONResponse) EachKey

func (res *HttpJSONResponse) EachKey(cb func(int, []byte, jsonparser.ValueType, error), paths ...[]string) int

func (*HttpJSONResponse) GetBoolean

func (res *HttpJSONResponse) GetBoolean(keys ...string) (val bool, err error)

func (*HttpJSONResponse) GetFloat

func (res *HttpJSONResponse) GetFloat(keys ...string) (val float64, err error)

func (*HttpJSONResponse) GetInt

func (res *HttpJSONResponse) GetInt(keys ...string) (val int64, err error)

func (*HttpJSONResponse) GetString

func (res *HttpJSONResponse) GetString(keys ...string) (val string, err error)

func (*HttpJSONResponse) GetUnsafeString

func (res *HttpJSONResponse) GetUnsafeString(keys ...string) (val string, err error)

func (*HttpJSONResponse) ObjectEach

func (res *HttpJSONResponse) ObjectEach(callback func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error, keys ...string) (err error)

func (*HttpJSONResponse) ParseString

func (res *HttpJSONResponse) ParseString() (string, error)

func (*HttpJSONResponse) Set

func (res *HttpJSONResponse) Set(setValue []byte, keys ...string) (value []byte, err error)

type HttpResponse

type HttpResponse struct {
	*http.Response
}

func (*HttpResponse) Bytes

func (res *HttpResponse) Bytes() ([]byte, error)

func (*HttpResponse) Discard

func (res *HttpResponse) Discard() error

func (*HttpResponse) HTML

func (res *HttpResponse) HTML() (*soup.Root, error)

func (*HttpResponse) JSON

func (res *HttpResponse) JSON() (*HttpJSONResponse, error)

func (*HttpResponse) Object

func (res *HttpResponse) Object(o interface{}) error

func (*HttpResponse) SaveToFile

func (res *HttpResponse) SaveToFile(filename string, permissions os.FileMode, overwrite, append bool) error

func (*HttpResponse) Text

func (res *HttpResponse) Text() (string, error)

func (*HttpResponse) XML

func (res *HttpResponse) XML() (*etree.Document, error)

type M3U8File

type M3U8File struct {
	Duration float64
	Location string
}

type M3U8FileList

type M3U8FileList struct {
	Files     []M3U8File
	RawValues map[string]string
}

type M3U8Index

type M3U8Index struct {
	PlayLists []M3U8PlayList
	RawValues map[string]string
}

type M3U8PlayList

type M3U8PlayList struct {
	URL         string
	Bandwidth   int
	ResolutionX int
	ResolutionY int
	Codecs      []string
	Framerate   int
	RawValues   map[string]string
}

type RefererOptions

type RefererOptions struct {
	Update bool
	Use    bool
	Value  string
}

type SSLPin

type SSLPin struct {
	SkipCA    bool
	Pins      []string //sha256
	Hostname  string
	Algorithm string
}

type SSLPinner

type SSLPinner struct {
	SSLPins map[string]SSLPin
}

func GetSSLPinner

func GetSSLPinner() SSLPinner

func (*SSLPinner) AddPin

func (p *SSLPinner) AddPin(hostname string, skipCA bool, pins ...string) error

func (*SSLPinner) GetPinsForHost

func (p *SSLPinner) GetPinsForHost(hostname string) (*SSLPin, error)

type Task

type Task struct {
	Id              int
	Name            string
	Total           uint64 // Whole file range downloaded as of yet
	Expected        uint64 // Whole file size
	FragSize        uint64 // If it's a fragment, the total size of the fragment
	CanResume       bool
	Threads         int
	URL             string
	ThreadObjects   []Task
	IsThread        bool
	ProgressChannel chan *TrackerMessage
}

func (*Task) Publish

func (t *Task) Publish(n int)

func (*Task) ReadyTask

func (t *Task) ReadyTask(resp *http.Response)

func (*Task) Write

func (t *Task) Write(p []byte) (int, error)

type TrackerMessage

type TrackerMessage struct {
	Total      uint64
	Delta      int
	Status     int
	Name       string
	Err        error
	Expected   uint64
	Id         int
	IsFragment bool
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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