1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-29 23:36:25 -04:00

refine http header parsing

This commit is contained in:
Darien Raymond 2016-11-06 15:48:25 +01:00
parent d70b997d84
commit 9f68062d48
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169

View File

@ -35,9 +35,9 @@ func (UTPAuthenticator) Build() (*loader.TypedSettings, error) {
} }
type HTTPAuthenticatorRequest struct { type HTTPAuthenticatorRequest struct {
Version *string `json:"version"` Version string `json:"version"`
Method *string `json:"method"` Method string `json:"method"`
Path *StringList `json:"path"` Path StringList `json:"path"`
Headers map[string]*StringList `json:"headers"` Headers map[string]*StringList `json:"headers"`
} }
@ -71,21 +71,24 @@ func (this *HTTPAuthenticatorRequest) Build() (*http.RequestConfig, error) {
}, },
} }
if this.Version != nil { if len(this.Version) > 0 {
config.Version = &http.Version{Value: *this.Version} config.Version = &http.Version{Value: this.Version}
} }
if this.Method != nil { if len(this.Method) > 0 {
config.Method = &http.Method{Value: *this.Method} config.Method = &http.Method{Value: this.Method}
} }
if this.Path != nil && this.Path.Len() > 0 { if len(this.Path) > 0 {
config.Uri = append([]string(nil), (*this.Path)...) config.Uri = append([]string(nil), (this.Path)...)
} }
if len(this.Headers) > 0 { if len(this.Headers) > 0 {
config.Header = make([]*http.Header, 0, len(this.Headers)) config.Header = make([]*http.Header, 0, len(this.Headers))
for key, value := range this.Headers { for key, value := range this.Headers {
if value == nil {
return nil, errors.New("Empty HTTP header value: " + key)
}
config.Header = append(config.Header, &http.Header{ config.Header = append(config.Header, &http.Header{
Name: key, Name: key,
Value: append([]string(nil), (*value)...), Value: append([]string(nil), (*value)...),
@ -97,9 +100,9 @@ func (this *HTTPAuthenticatorRequest) Build() (*http.RequestConfig, error) {
} }
type HTTPAuthenticatorResponse struct { type HTTPAuthenticatorResponse struct {
Version *string `json:"version"` Version string `json:"version"`
Status *string `json:"status"` Status string `json:"status"`
Reason *string `json:"reason"` Reason string `json:"reason"`
Headers map[string]*StringList `json:"headers"` Headers map[string]*StringList `json:"headers"`
} }
@ -125,26 +128,29 @@ func (this *HTTPAuthenticatorResponse) Build() (*http.ResponseConfig, error) {
}, },
} }
if this.Version != nil { if len(this.Version) > 0 {
config.Version = &http.Version{Value: *this.Version} config.Version = &http.Version{Value: this.Version}
} }
if this.Status != nil || this.Reason != nil { if len(this.Status) > 0 || len(this.Reason) > 0 {
config.Status = &http.Status{ config.Status = &http.Status{
Code: "200", Code: "200",
Reason: "OK", Reason: "OK",
} }
if this.Status != nil { if len(this.Status) > 0 {
config.Status.Code = *this.Status config.Status.Code = this.Status
} }
if this.Reason != nil { if len(this.Reason) > 0 {
config.Status.Reason = *this.Reason config.Status.Reason = this.Reason
} }
} }
if len(this.Headers) > 0 { if len(this.Headers) > 0 {
config.Header = make([]*http.Header, 0, len(this.Headers)) config.Header = make([]*http.Header, 0, len(this.Headers))
for key, value := range this.Headers { for key, value := range this.Headers {
if value == nil {
return nil, errors.New("Empty HTTP header value: " + key)
}
config.Header = append(config.Header, &http.Header{ config.Header = append(config.Header, &http.Header{
Name: key, Name: key,
Value: append([]string(nil), (*value)...), Value: append([]string(nil), (*value)...),
@ -156,28 +162,23 @@ func (this *HTTPAuthenticatorResponse) Build() (*http.ResponseConfig, error) {
} }
type HTTPAuthenticator struct { type HTTPAuthenticator struct {
Request *HTTPAuthenticatorRequest `json:"request"` Request HTTPAuthenticatorRequest `json:"request"`
Response *HTTPAuthenticatorResponse `json:"response"` Response HTTPAuthenticatorResponse `json:"response"`
} }
func (this *HTTPAuthenticator) Build() (*loader.TypedSettings, error) { func (this *HTTPAuthenticator) Build() (*loader.TypedSettings, error) {
config := new(http.Config) config := new(http.Config)
if this.Request == nil {
return nil, errors.New("HTTP request settings not set.")
}
requestConfig, err := this.Request.Build() requestConfig, err := this.Request.Build()
if err != nil { if err != nil {
return nil, err return nil, err
} }
config.Request = requestConfig config.Request = requestConfig
if this.Response != nil {
responseConfig, err := this.Response.Build() responseConfig, err := this.Response.Build()
if err != nil { if err != nil {
return nil, err return nil, err
} }
config.Response = responseConfig config.Response = responseConfig
}
return loader.NewTypedSettings(config), nil return loader.NewTypedSettings(config), nil
} }