refactor ciims proxy transport and handlers

This commit is contained in:
zhiqiang feng
2026-07-08 15:17:07 +08:00
parent 04bafe01e7
commit dd3f6769f6
8 changed files with 689 additions and 154 deletions
+69 -21
View File
@@ -1,8 +1,8 @@
package internal
import (
"bytes"
"io/ioutil"
"fmt"
"io"
"net/http"
"strings"
"time"
@@ -13,37 +13,85 @@ const (
defaultAgent string = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; XFire Client +http://xfire.codehaus.org)"
)
//Post xml string to url
func post(url string, contentType string, msg string) (*http.Response, error) {
return http.Post(url, contentType, bytes.NewBuffer([]byte(msg)))
type Client struct {
httpClient *http.Client
}
func postSim(url string, msg string) (string, error) {
timeout := time.Duration(10 * time.Second)
client := http.Client{
Timeout: timeout,
}
type CIIMSResponse struct {
RawXML string
}
type HTTPStatusError struct {
StatusCode int
Status string
Body string
}
func (e *HTTPStatusError) Error() string {
body := e.Body
if len(body) > 512 {
body = body[:512] + "..."
}
if body == "" {
return fmt.Sprintf("ciims returned %s", e.Status)
}
return fmt.Sprintf("ciims returned %s: %s", e.Status, body)
}
func NewClient(timeoutSec int) (*Client, error) {
if timeoutSec <= 0 {
return nil, fmt.Errorf("timeout must be positive")
}
return &Client{
httpClient: &http.Client{
Timeout: time.Duration(timeoutSec) * time.Second,
},
}, nil
}
func (r *CIIMSResponse) IsFault() bool {
return r != nil && r.ErrorMessage() != ""
}
func (r *CIIMSResponse) ErrorMessage() string {
if r == nil {
return ""
}
return GetErrMsg(r.RawXML)
}
func (r *CIIMSResponse) Messages() []string {
if r == nil {
return nil
}
return GetMsgs(r.RawXML)
}
func (c *Client) Send(url string, msg string) (*CIIMSResponse, error) {
req, err := http.NewRequest(http.MethodPost, url, strings.NewReader(msg))
if err != nil {
return err.Error(), err
return nil, err
}
req.Header.Set("Content-Type", defaultContentType)
req.Header.Set("User-Agent", defaultAgent)
req.Header.Set("SOAPAction", "")
resp, err := client.Do(req)
resp, err := c.httpClient.Do(req)
if err != nil {
return err.Error(), err
return nil, err
}
defer resp.Body.Close()
respBytes, err := ioutil.ReadAll(resp.Body)
respBytes, err := io.ReadAll(resp.Body)
if err != nil {
return err.Error(), err
return nil, err
}
return string(respBytes), nil
}
//Send message
func Send(url string, message string) (string, error) {
return postSim(url, message)
body := string(respBytes)
ciimsResp := &CIIMSResponse{RawXML: body}
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
return ciimsResp, &HTTPStatusError{
StatusCode: resp.StatusCode,
Status: resp.Status,
Body: body,
}
}
return ciimsResp, nil
}