Пример #1
0
func main() {

	client := wdhttp.NewHTTPClient()

	//data := "jVuWGxduPYR0+sJ7ZOHlf3cRliE2o9b8dA5rEF7sTl4zkRkutWjNb+OalJih8GKulVSsSabwrUteQlSOZzwKOOVRf8h3h8Y2zL8Y5w2PBoFct9nPYjvLEmlm/M/f9MgZZePRmJhUNmbTyedZ1MrfgpXtJEbJabRZYR2ojamOnIQ2P9k6+B04p4Tl07BtnI2n"

	conf := &wdhttp.ClientConfig{
		Redirect: false,
		Header:   map[string]string{"Authorization": "Basic Z2Vycml0d2Q6d2UncmUjMQ=="},

		Url:    "https://git.wandoulabs.com/#/q/status:open",
		Method: "GET",
	}

	req, _ := wdhttp.SetupNewRequest(conf)
	t := &wdhttp.Timeout{
		ConnectTimeout: 10,
		HeaderTimeout:  10,
		TotalTimeout:   10,
	}
	client.PrepareTimeout(t)
	client.PrepareRedirect(false)
	r, _ := client.ExecRequest(req)

	if r.Code > 200 && r.Code < 400 && conf.Method == "POST" {
		conf.Method = "GET"
		req, _ = wdhttp.SetupNewRequest(conf)
		client.PrepareTimeout(t)
		client.PrepareRedirect(true)
		r, _ = client.ExecRequest(req)
	}
	fmt.Printf("%+v\n", r)

}
Пример #2
0
func (h *HTTPMonitor) HTTPCurlRoutine(myURL string, c *confInfo) {
	client := wdhttp.NewHTTPClient()
	conf := &wdhttp.ClientConfig{
		Header:   c.Header,
		Url:      myURL,
		Protocol: c.Prot,
		Method:   c.Method,
	}

	var isRedirect bool

	if c.Redirect == 1 {
		isRedirect = true
	} else {
		isRedirect = false
	}

	body := make(map[string]string)

	if c.Data != nil {

		t := reflect.TypeOf(c.Data).Kind()
		switch {
		case t == reflect.Map:
			for k, v := range c.Data.(map[string]interface{}) {
				vType := reflect.TypeOf(v).Kind()
				switch vType {
				case reflect.String:
					body[k] = v.(string)
				case reflect.Int:
					body[k] = fmt.Sprintf("%d", v.(int))
				case reflect.Float64:
					body[k] = fmt.Sprintf("%d", int(v.(float64)))
				}
			}
			conf.MapBody = body
		case t == reflect.String:
			conf.StrBody = c.Data.(string)
		}
	}

	t := &wdhttp.Timeout{
		ConnectTimeout: c.Interval / 5,
		HeaderTimeout:  c.Interval / 3,
		TotalTimeout:   c.Interval / 2,
	}

	req, err := wdhttp.SetupNewRequest(conf)
	if err != nil {
		glog.Errorf("Setup New Request Failed for conf %+v as %s\n", conf, err)
		return
	}

	client.PrepareTimeout(t)
	client.PrepareRedirect(isRedirect)

	r, err := client.ExecRequest(req)
	if err != nil {
		glog.Errorf("Exec Request Failed for conf %+v as %s\n", conf, err)
		return
	}

	// in order to make compatible with HTTP interface which not support POST method,
	// retry the request using GET HTTP method
	if r.Code > 200 && r.Code < 400 && conf.Method == "POST" {
		conf.Method = "GET"
		req, err = wdhttp.SetupNewRequest(conf)
		if err != nil {
			glog.Errorf("Setup New Request Failed for conf %+v as %s\n", conf, err)
			return
		}
		client.PrepareTimeout(t)
		client.PrepareRedirect(true)
		r, err = client.ExecRequest(req)
		if err != nil {
			glog.Errorf("Exec Request Failed for conf %+v as %s\n", conf, err)
			return
		}
	}

	respStringnify := func(h http.Header) (s string, err error) {
		buffer := new(bytes.Buffer)
		err = h.Write(buffer)
		if err != nil {
			return "", err
		}
		s = buffer.String()
		return s, nil
	}

	_responseHeader, err := respStringnify(r.ResponseHeader)
	if err != nil {
		glog.Errorf("monitor url %s stringnify response header failed as %s\n", myURL, err)
		return
	}

	ret := retInfo{
		Content:        r.Content,
		Length:         r.Length,
		Code:           r.Code,
		RequestTime:    r.RequestTime,
		ResponseHeader: _responseHeader,
	}

	if r.Length > 2000000 {
		ret.Content = "content too long"
	}

	kr := &keyRet{
		Data:  ret,
		Stamp: time.Now().Unix(),
	}

	j, err := json.Marshal(kr)
	if err != nil {
		glog.Errorf("monitor url %s result can't been marshaled as Json: %s", err)
		return
	}

	item := fmt.Sprintf("result:%s", c.Name)

	// clean ret's Content for clearly logging info
	ret.Content = ""
	glog.Infof("fetch url %s succeed, send result %s to deQueue:\n %+v \n", myURL, item, ret)
	h.deQueue <- []string{item, myURL, string(j)}
}