-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
159 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,19 @@ | ||
module github.com/fogfish/gurl/v2 | ||
|
||
go 1.21 | ||
go 1.23 | ||
|
||
toolchain go1.23.1 | ||
|
||
require ( | ||
github.com/ajg/form v1.5.2-0.20200323032839-9aeb3cf462e1 | ||
github.com/fogfish/it/v2 v2.0.1 | ||
github.com/fogfish/it/v2 v2.0.2 | ||
golang.org/x/net v0.17.0 | ||
) | ||
|
||
require github.com/google/go-cmp v0.6.0 | ||
|
||
require ( | ||
github.com/fogfish/golem/hseq v1.2.0 // indirect | ||
github.com/fogfish/golem/optics v0.13.1 // indirect | ||
github.com/fogfish/opts v0.0.2 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// | ||
// Copyright (C) 2019 - 2024 Dmitry Kolesnikov | ||
// | ||
// This file may be modified and distributed under the terms | ||
// of the MIT license. See the LICENSE file for details. | ||
// https://github.com/fogfish/gurl | ||
// | ||
|
||
package http | ||
|
||
import ( | ||
"crypto/tls" | ||
"fmt" | ||
"net/http" | ||
"net/http/cookiejar" | ||
|
||
"github.com/fogfish/opts" | ||
"golang.org/x/net/publicsuffix" | ||
) | ||
|
||
// HTTP Stack config option | ||
type Option = opts.Option[Protocol] | ||
|
||
var ( | ||
// Set custom implementation of HTTP client. | ||
// It requires anything that implements Socket interface (aka http.Client) | ||
// | ||
// type Socket interface { | ||
// Do(req *http.Request) (*http.Response, error) | ||
// } | ||
WithClient = opts.ForType[Protocol, Socket]() | ||
|
||
// Set the default host for http stack. | ||
// The host is used when request URI does not contain any host. | ||
WithHost = opts.ForName[Protocol, string]("Host") | ||
|
||
// Enables HTTP Response buffering | ||
WithMemento = opts.ForName[Protocol, bool]("Memento") | ||
|
||
// Buffers HTTP Response Payload into context. | ||
WithMementoPayload = WithMemento(true) | ||
|
||
// Disables TLS certificate validation for HTTP(S) sessions. | ||
WithInsecureTLS = opts.From(withInsecureTLS) | ||
|
||
// Enables automated cookie handling across requests originated from the session. | ||
WithCookieJar = opts.From(withCookieJar) | ||
|
||
// Disables default [gurl] redirect policy to Golang's one. | ||
// It enables the HTTP stack automatically follows redirects | ||
WithRedirects = opts.From(withRedirects) | ||
|
||
// Enable log level | ||
WithLogLevel = opts.ForName[Protocol, int]("LogLevel") | ||
|
||
// Enables debug logging. | ||
// The logger outputs HTTP requests only. | ||
WithDebugRequest = WithLogLevel(1) | ||
|
||
// Enables debug logging. | ||
// The logger outputs HTTP requests and responses. | ||
WithDebugResponse = WithLogLevel(2) | ||
|
||
// Enable debug logging. | ||
WithDebugPayload = WithLogLevel(3) | ||
) | ||
|
||
func withInsecureTLS(cat *Protocol) error { | ||
cli, ok := cat.Socket.(*http.Client) | ||
if !ok { | ||
return fmt.Errorf("unsupported transport type %T", cat) | ||
} | ||
|
||
switch t := cli.Transport.(type) { | ||
case *http.Transport: | ||
if t.TLSClientConfig == nil { | ||
t.TLSClientConfig = &tls.Config{} | ||
} | ||
t.TLSClientConfig.InsecureSkipVerify = true | ||
return nil | ||
default: | ||
return fmt.Errorf("unsupported transport type %T", t) | ||
} | ||
} | ||
|
||
func withCookieJar(cat *Protocol) error { | ||
cli, ok := cat.Socket.(*http.Client) | ||
if !ok { | ||
return fmt.Errorf("unsupported transport type %T", cat) | ||
} | ||
|
||
jar, err := cookiejar.New(&cookiejar.Options{ | ||
PublicSuffixList: publicsuffix.List, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
cli.Jar = jar | ||
|
||
return nil | ||
} | ||
|
||
func withRedirects(cat *Protocol) error { | ||
cli, ok := cat.Socket.(*http.Client) | ||
if !ok { | ||
return fmt.Errorf("unsupported transport type %T", cat) | ||
} | ||
|
||
cli.CheckRedirect = nil | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.