當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Response.FindStringSubmatch方法代碼示例

本文整理匯總了Golang中net/http.Response.FindStringSubmatch方法的典型用法代碼示例。如果您正苦於以下問題:Golang Response.FindStringSubmatch方法的具體用法?Golang Response.FindStringSubmatch怎麽用?Golang Response.FindStringSubmatch使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在net/http.Response的用法示例。


在下文中一共展示了Response.FindStringSubmatch方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: sendArticle

func (h *httpWebhook) sendArticle(msgid nntp.MessageID, group nntp.Newsgroup) {
	f, err := h.storage.OpenArticle(msgid.String())
	if err == nil {
		u, _ := url.Parse(h.conf.URL)
		var r *http.Response
		var ctype string
		if h.conf.Dialect == "vichan" {
			c := textproto.NewConn(f)
			var hdr textproto.MIMEHeader
			hdr, err = c.ReadMIMEHeader()
			if err == nil {
				var body io.Reader
				ctype = hdr.Get("Content-Type")
				if ctype == "" || strings.HasPrefix(ctype, "text/plain") {
					ctype = "text/plain"
				}
				ctype = strings.Replace(strings.ToLower(ctype), "multipart/mixed", "multipart/form-data", 1)
				q := u.Query()
				for k, vs := range hdr {
					for _, v := range vs {
						q.Add(k, v)
					}
				}
				q.Set("Content-Type", ctype)
				u.RawQuery = q.Encode()

				if strings.HasPrefix(ctype, "multipart") {
					pr, pw := io.Pipe()
					log.Debug("using pipe")
					go func(in io.Reader, out io.WriteCloser) {
						_, params, _ := mime.ParseMediaType(ctype)
						if params == nil {
							// send as whatever lol
							io.Copy(out, in)
						} else {
							boundary, _ := params["boundary"]
							mpr := multipart.NewReader(in, boundary)
							mpw := multipart.NewWriter(out)
							mpw.SetBoundary(boundary)
							for {
								part, err := mpr.NextPart()
								if err == io.EOF {
									err = nil
									break
								} else if err == nil {
									// get part header
									h := part.Header
									// rewrite header part for php
									cd := h.Get("Content-Disposition")
									r := regexp.MustCompile(`filename="(.*)"`)
									// YOLO
									parts := r.FindStringSubmatch(cd)
									if len(parts) > 1 {
										fname := parts[1]
										h.Set("Content-Disposition", fmt.Sprintf(`filename="%s"; name="attachment[]"`, fname))
									}
									// make write part
									wp, err := mpw.CreatePart(h)
									if err == nil {
										// write part out
										io.Copy(wp, part)
									} else {
										log.Errorf("error writng webhook part: %s", err.Error())
									}
								}
								part.Close()
							}
							mpw.Close()
						}
						out.Close()
					}(c.R, pw)
					body = pr
				} else {
					body = f
				}
				r, err = http.Post(u.String(), ctype, body)
			}
		} else {
			var sz int64
			sz, err = f.Seek(0, 2)
			if err != nil {
				return
			}
			f.Seek(0, 0)
			// regular webhook
			ctype = "text/plain; charset=UTF-8"
			cl := new(http.Client)
			r, err = cl.Do(&http.Request{
				ContentLength: sz,
				URL:           u,
				Method:        "POST",
				Body:          f,
			})
		}
		if err == nil && r != nil {
			dec := json.NewDecoder(r.Body)
			result := make(map[string]interface{})
			err = dec.Decode(&result)
			if err == nil || err == io.EOF {
				msg, ok := result["error"]
//.........這裏部分代碼省略.........
開發者ID:majestrate,項目名稱:nntpchan,代碼行數:101,代碼來源:http.go


注:本文中的net/http.Response.FindStringSubmatch方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。