forked from hprose/hprose-golang
/
basehttp_service.go
113 lines (99 loc) · 4.1 KB
/
basehttp_service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**********************************************************\
| |
| hprose |
| |
| Official WebSite: http://www.hprose.com/ |
| http://www.hprose.org/ |
| |
\**********************************************************/
/**********************************************************\
* *
* rpc/basehttp_service.go *
* *
* hprose basehttp service for Go. *
* *
* LastModified: Nov 1, 2016 *
* Author: Ma Bingyao <andot@hprose.com> *
* *
\**********************************************************/
package rpc
import (
"io/ioutil"
"math/rand"
"strconv"
"time"
)
// BaseHTTPService is the hprose base http service
type BaseHTTPService struct {
BaseService
P3P bool
GET bool
CrossDomain bool
AccessControlAllowOrigins map[string]bool
LastModified string
Etag string
crossDomainXMLFile string
crossDomainXMLContent []byte
clientAccessPolicyXMLFile string
clientAccessPolicyXMLContent []byte
}
// InitBaseHTTPService initializes BaseHTTPService
func (service *BaseHTTPService) InitBaseHTTPService() {
t := time.Now().UTC()
rand.Seed(t.UnixNano())
service.InitBaseService()
service.P3P = true
service.GET = true
service.CrossDomain = true
service.AccessControlAllowOrigins = make(map[string]bool)
service.LastModified = t.Format(time.RFC1123)
service.Etag = `"` + strconv.FormatInt(rand.Int63(), 16) + `"`
}
// AddAccessControlAllowOrigin add access control allow origin
func (service *BaseHTTPService) AddAccessControlAllowOrigin(origins ...string) {
for _, origin := range origins {
service.AccessControlAllowOrigins[origin] = true
}
}
// RemoveAccessControlAllowOrigin remove access control allow origin
func (service *BaseHTTPService) RemoveAccessControlAllowOrigin(origins ...string) {
for _, origin := range origins {
delete(service.AccessControlAllowOrigins, origin)
}
}
// CrossDomainXMLFile return the cross domain xml file
func (service *BaseHTTPService) CrossDomainXMLFile() string {
return service.crossDomainXMLFile
}
// CrossDomainXMLContent return the cross domain xml content
func (service *BaseHTTPService) CrossDomainXMLContent() []byte {
return service.crossDomainXMLContent
}
// ClientAccessPolicyXMLFile return the client access policy xml file
func (service *BaseHTTPService) ClientAccessPolicyXMLFile() string {
return service.clientAccessPolicyXMLFile
}
// ClientAccessPolicyXMLContent return the client access policy xml content
func (service *BaseHTTPService) ClientAccessPolicyXMLContent() []byte {
return service.clientAccessPolicyXMLContent
}
// SetCrossDomainXMLFile set the cross domain xml file
func (service *BaseHTTPService) SetCrossDomainXMLFile(filename string) {
service.crossDomainXMLFile = filename
service.crossDomainXMLContent, _ = ioutil.ReadFile(filename)
}
// SetClientAccessPolicyXMLFile set the client access policy xml file
func (service *BaseHTTPService) SetClientAccessPolicyXMLFile(filename string) {
service.clientAccessPolicyXMLFile = filename
service.clientAccessPolicyXMLContent, _ = ioutil.ReadFile(filename)
}
// SetCrossDomainXMLContent set the cross domain xml content
func (service *BaseHTTPService) SetCrossDomainXMLContent(content []byte) {
service.crossDomainXMLFile = ""
service.crossDomainXMLContent = content
}
// SetClientAccessPolicyXMLContent set the client access policy xml content
func (service *BaseHTTPService) SetClientAccessPolicyXMLContent(content []byte) {
service.clientAccessPolicyXMLFile = ""
service.clientAccessPolicyXMLContent = content
}