simplify http header parsing

pull/314/head
Darien Raymond 2016-11-05 15:14:55 +01:00
parent f108633e2e
commit 31be091a55
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
1 changed files with 15 additions and 31 deletions

View File

@ -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"
@ -131,41 +129,27 @@ func (this *HTTPAuthenticatorResponse) Build() (*http.ResponseConfig, error) {
}
type HTTPAuthenticator struct {
Request *HTTPAuthenticatorRequest `json:"request"`
Response json.RawMessage `json:"response"`
Request *HTTPAuthenticatorRequest `json:"request"`
Response *HTTPAuthenticatorResponse `json:"response"`
}
func (this *HTTPAuthenticator) Build() (*loader.TypedSettings, error) {
config := new(http.Config)
if this.Request != nil {
requestConfig, err := this.Request.Build()
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 this.Response != nil {
responseConfig, err := this.Response.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 err != nil {
return nil, err
}
config.Response = responseConfig
}
config.Response = responseConfig
}
return loader.NewTypedSettings(config), nil