Skip to content

Commit

Permalink
turn internalConfig methods into functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jub0bs committed Jan 26, 2025
1 parent 50e0f6a commit 9467162
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
24 changes: 12 additions & 12 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ func newInternalConfig(cfg *Config) (*internalConfig, error) {
)

// extra config (accessed by other validateX methods)
if err := icfg.validatePreflightStatus(cfg.PreflightSuccessStatus); err != nil {
if err := validatePreflightStatus(&icfg, cfg.PreflightSuccessStatus); err != nil {
errs = append(errs, err)
}
if cfg.PrivateNetworkAccess && cfg.PrivateNetworkAccessInNoCORSModeOnly {
Expand All @@ -489,19 +489,19 @@ func newInternalConfig(cfg *Config) (*internalConfig, error) {

// base config
icfg.credentialed = cfg.Credentialed // accessed by other validateX methods
if err := icfg.validateOrigins(cfg.Origins); err != nil {
if err := validateOrigins(&icfg, cfg.Origins); err != nil {
errs = append(errs, err)
}
if err := icfg.validateMethods(cfg.Methods); err != nil {
if err := validateMethods(&icfg, cfg.Methods); err != nil {
errs = append(errs, err)
}
if err := icfg.validateRequestHeaders(cfg.RequestHeaders); err != nil {
if err := validateRequestHeaders(&icfg, cfg.RequestHeaders); err != nil {
errs = append(errs, err)
}
if err := icfg.validateMaxAge(cfg.MaxAgeInSeconds); err != nil {
if err := validateMaxAge(&icfg, cfg.MaxAgeInSeconds); err != nil {
errs = append(errs, err)
}
if err := icfg.validateResponseHeaders(cfg.ResponseHeaders); err != nil {
if err := validateResponseHeaders(&icfg, cfg.ResponseHeaders); err != nil {
errs = append(errs, err)
}

Expand All @@ -511,7 +511,7 @@ func newInternalConfig(cfg *Config) (*internalConfig, error) {
return &icfg, nil
}

func (icfg *internalConfig) validateOrigins(patterns []string) error {
func validateOrigins(icfg *internalConfig, patterns []string) error {
if len(patterns) == 0 {
err := &cfgerrors.UnacceptableOriginPatternError{
Reason: "missing",
Expand Down Expand Up @@ -601,7 +601,7 @@ func (icfg *internalConfig) validateOrigins(patterns []string) error {
return nil
}

func (icfg *internalConfig) validateMethods(names []string) error {
func validateMethods(icfg *internalConfig, names []string) error {
if len(names) == 0 {
return nil
}
Expand Down Expand Up @@ -651,7 +651,7 @@ func (icfg *internalConfig) validateMethods(names []string) error {
return nil
}

func (icfg *internalConfig) validateRequestHeaders(names []string) error {
func validateRequestHeaders(icfg *internalConfig, names []string) error {
if len(names) == 0 {
return nil
}
Expand Down Expand Up @@ -728,7 +728,7 @@ func (icfg *internalConfig) validateRequestHeaders(names []string) error {
return nil
}

func (icfg *internalConfig) validateMaxAge(delta int) error {
func validateMaxAge(icfg *internalConfig, delta int) error {
const (
// see https://fetch.spec.whatwg.org/#cors-preflight-fetch-0, step 7.9
defaultMaxAge = 5
Expand Down Expand Up @@ -760,7 +760,7 @@ func (icfg *internalConfig) validateMaxAge(delta int) error {
}
}

func (icfg *internalConfig) validateResponseHeaders(names []string) error {
func validateResponseHeaders(icfg *internalConfig, names []string) error {
if len(names) == 0 {
return nil
}
Expand Down Expand Up @@ -830,7 +830,7 @@ func (icfg *internalConfig) validateResponseHeaders(names []string) error {
return nil
}

func (icfg *internalConfig) validatePreflightStatus(status int) error {
func validatePreflightStatus(icfg *internalConfig, status int) error {
if status == 0 {
icfg.preflightStatusMinus200 = defaultPreflightStatus - 200
return nil
Expand Down
28 changes: 16 additions & 12 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (m *Middleware) Wrap(h http.Handler) http.Handler {
if !found {
// r is NOT a CORS request;
// see https://fetch.spec.whatwg.org/#cors-request.
icfg.handleNonCORS(w.Header(), isOPTIONS)
handleNonCORS(icfg, w.Header(), isOPTIONS)
h.ServeHTTP(w, r)
return
}
Expand All @@ -151,16 +151,16 @@ func (m *Middleware) Wrap(h http.Handler) http.Handler {
if isOPTIONS && found {
// r is a CORS-preflight request;
// see https://fetch.spec.whatwg.org/#cors-preflight-request.
icfg.handleCORSPreflight(w, r.Header, origin, originSgl, acrm, acrmSgl, debug)
handleCORSPreflight(icfg, w, r.Header, origin, originSgl, acrm, acrmSgl, debug)
return
}
// r is an "actual" (i.e. non-preflight) CORS request.
icfg.handleCORSActual(w, origin, originSgl, isOPTIONS)
handleCORSActual(icfg, w, origin, originSgl, isOPTIONS)
h.ServeHTTP(w, r)
})
}

func (icfg *internalConfig) handleNonCORS(resHdrs http.Header, isOPTIONS bool) {
func handleNonCORS(icfg *internalConfig, resHdrs http.Header, isOPTIONS bool) {
if isOPTIONS {
// see the implementation comment in handleCORSPreflight
resHdrs.Add(headers.Vary, headers.ValueVaryOptions)
Expand All @@ -187,7 +187,8 @@ func (icfg *internalConfig) handleNonCORS(resHdrs http.Header, isOPTIONS bool) {
}
}

func (icfg *internalConfig) handleCORSPreflight(
func handleCORSPreflight(
icfg *internalConfig,
w http.ResponseWriter,
reqHdrs http.Header,
origin string,
Expand Down Expand Up @@ -234,7 +235,7 @@ func (icfg *internalConfig) handleCORSPreflight(

// For details about the order in which we perform the following checks,
// see https://fetch.spec.whatwg.org/#cors-preflight-fetch, item 7.
if !icfg.processOriginForPreflight(buf, origin, originSgl) {
if !processOriginForPreflight(icfg, buf, origin, originSgl) {
if debug {
maps.Copy(resHdrs, buf)
}
Expand All @@ -246,7 +247,7 @@ func (icfg *internalConfig) handleCORSPreflight(
// (see https://fetch.spec.whatwg.org/#cors-preflight-fetch-0, step 7)
// if the response status is not an ok status
// (see https://fetch.spec.whatwg.org/#ok-status).
if !icfg.processACRPN(buf, reqHdrs) {
if !processACRPN(icfg, buf, reqHdrs) {
if debug {
maps.Copy(resHdrs, buf)
w.WriteHeader(int(icfg.preflightStatusMinus200) + 200)
Expand All @@ -256,7 +257,7 @@ func (icfg *internalConfig) handleCORSPreflight(
return
}

if !icfg.processACRM(buf, acrm, acrmSgl) {
if !processACRM(icfg, buf, acrm, acrmSgl) {
if debug {
maps.Copy(resHdrs, buf)
w.WriteHeader(int(icfg.preflightStatusMinus200) + 200)
Expand Down Expand Up @@ -284,7 +285,8 @@ func (icfg *internalConfig) handleCORSPreflight(
w.WriteHeader(int(icfg.preflightStatusMinus200) + 200)
}

func (icfg *internalConfig) processOriginForPreflight(
func processOriginForPreflight(
icfg *internalConfig,
buf http.Header,
origin string,
originSgl []string,
Expand All @@ -310,7 +312,7 @@ func (icfg *internalConfig) processOriginForPreflight(
return true
}

func (icfg *internalConfig) processACRPN(buf, reqHdrs http.Header) bool {
func processACRPN(icfg *internalConfig, buf, reqHdrs http.Header) bool {
// See https://wicg.github.io/private-network-access/#cors-preflight.
//
// PNA-compliant browsers send at most one ACRPN header;
Expand All @@ -328,7 +330,8 @@ func (icfg *internalConfig) processACRPN(buf, reqHdrs http.Header) bool {
}

// Note: only for _non-preflight_ CORS requests
func (icfg *internalConfig) handleCORSActual(
func handleCORSActual(
icfg *internalConfig,
w http.ResponseWriter,
origin string,
originSgl []string,
Expand Down Expand Up @@ -384,7 +387,8 @@ func (icfg *internalConfig) handleCORSActual(
}
}

func (icfg *internalConfig) processACRM(
func processACRM(
icfg *internalConfig,
buf http.Header,
acrm string,
acrmSgl []string,
Expand Down

0 comments on commit 9467162

Please sign in to comment.