mirror of https://github.com/v2ray/v2ray-core
				
				
				
			simplify http header parsing
							parent
							
								
									f108633e2e
								
							
						
					
					
						commit
						31be091a55
					
				| 
						 | 
				
			
			@ -1,10 +1,8 @@
 | 
			
		|||
package conf
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
 | 
			
		||||
	"errors"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"v2ray.com/core/common/loader"
 | 
			
		||||
	"v2ray.com/core/transport/internet/authenticators/http"
 | 
			
		||||
	"v2ray.com/core/transport/internet/authenticators/noop"
 | 
			
		||||
| 
						 | 
				
			
			@ -132,41 +130,27 @@ func (this *HTTPAuthenticatorResponse) Build() (*http.ResponseConfig, error) {
 | 
			
		|||
 | 
			
		||||
type HTTPAuthenticator struct {
 | 
			
		||||
	Request  *HTTPAuthenticatorRequest  `json:"request"`
 | 
			
		||||
	Response json.RawMessage           `json:"response"`
 | 
			
		||||
	Response *HTTPAuthenticatorResponse `json:"response"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *HTTPAuthenticator) Build() (*loader.TypedSettings, error) {
 | 
			
		||||
	config := new(http.Config)
 | 
			
		||||
	if this.Request != nil {
 | 
			
		||||
	if this.Request == nil {
 | 
			
		||||
		return nil, errors.New("HTTP request settings not set.")
 | 
			
		||||
	}
 | 
			
		||||
	requestConfig, err := this.Request.Build()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	config.Request = requestConfig
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if len(this.Response) > 0 {
 | 
			
		||||
		var text string
 | 
			
		||||
		parsed := false
 | 
			
		||||
		if err := json.Unmarshal(this.Response, &text); err == nil {
 | 
			
		||||
			if strings.ToLower(text) != "disabled" {
 | 
			
		||||
				return nil, errors.New("Unknown HTTP header settings: " + text)
 | 
			
		||||
			}
 | 
			
		||||
			parsed = true
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if !parsed {
 | 
			
		||||
			var response HTTPAuthenticatorResponse
 | 
			
		||||
			if err := json.Unmarshal(this.Response, &response); err != nil {
 | 
			
		||||
				return nil, errors.New("Failed to parse HTTP header response.")
 | 
			
		||||
			}
 | 
			
		||||
			responseConfig, err := response.Build()
 | 
			
		||||
	if this.Response != nil {
 | 
			
		||||
		responseConfig, err := this.Response.Build()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
		config.Response = responseConfig
 | 
			
		||||
	}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return loader.NewTypedSettings(config), nil
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue