Пример #1
0
// parseCookies set the sessionCookie via Javascript evaluation
func parseCookies(base *url.URL, js string, cookies http.CookieJar) {
	vm := otto.New()
	if result, err := vm.Run(js + "\nWEBVAR_JSONVAR_WEB_SESSION.WEBVAR_STRUCTNAME_WEB_SESSION[0].SESSION_COOKIE"); err == nil {
		cookie, _ := result.ToString()
		cookies.SetCookies(base, []*http.Cookie{{Name: "SessionCookie", Value: cookie}})
	} else {
		log.Fatalf("Error: %s\n", err)
	}
}
Пример #2
0
Файл: client.go Проект: cmars/oo
// SetCookie sets a cookie for the given URL on the given cookie jar
// that will holds the given macaroon slice. The macaroon slice should
// contain a single primary macaroon in its first element, and any
// discharges after that.
func SetCookie(jar http.CookieJar, url *url.URL, ms macaroon.Slice) error {
	cookie, err := NewCookie(ms)
	if err != nil {
		return errgo.Mask(err)
	}
	// TODO verify that setting this for the URL makes it available
	// to all paths under that URL.
	jar.SetCookies(url, []*http.Cookie{cookie})
	return nil
}
Пример #3
0
Файл: agent.go Проект: cmars/oo
// SetCookie creates a cookie in jar which is suitable for performing agent
// logins to u.
//
// If using SetUpAuth, it should not be necessary to use
// this function.
func SetCookie(jar http.CookieJar, u *url.URL, username string, pk *bakery.PublicKey) {
	al := agentLogin{
		Username:  username,
		PublicKey: pk,
	}
	b, err := json.Marshal(al)
	if err != nil {
		// This shouldn't happen as the agentLogin type has to be marshalable.
		panic(errgo.Notef(err, "cannot marshal cookie"))
	}
	v := base64.StdEncoding.EncodeToString(b)
	jar.SetCookies(u, []*http.Cookie{{
		Name:  cookieName,
		Value: v,
	}})
}
Пример #4
0
func cookie_process(cookiejar http.CookieJar, surl string, cookiedata string) {
	if cookiedata == "" {
		return
	}
	cookiedata = "HTTP/1.0 200 OK\r\n" + cookiedata + "\r\n\r\n"
	req, err := http.NewRequest("GET", surl, nil)
	if err != nil {
		fmt.Println(err)
		return
	}
	res, err := http.ReadResponse(bufio.NewReader(strings.NewReader(cookiedata)), req)
	if err != nil {
		fmt.Println(err)
		return
	}
	cookies := res.Cookies()
	turl, err := url.Parse(surl)
	cookiejar.SetCookies(turl, cookies)
}
Пример #5
0
// Start a request, and get the response.
//
// Usually we just need the Get and Post method.
func (this *HttpClient) Do(method string, url string, headers map[string]string,
	body io.Reader) (*Response, error) {
	options := mergeOptions(defaultOptions, this.Options, this.oneTimeOptions)
	headers = mergeHeaders(this.Headers, this.oneTimeHeaders, headers)
	cookies := this.oneTimeCookies

	var transport http.RoundTripper
	var jar http.CookieJar
	var err error

	// transport
	if this.transport == nil || !this.reuseTransport {
		transport, err = prepareTransport(options)
		if err != nil {
			this.reset()
			return nil, err
		}

		if this.reuseTransport {
			this.transport = transport
		}
	} else {
		transport = this.transport
	}

	// jar
	if this.jar == nil || !this.reuseJar {
		jar, err = prepareJar(options)
		if err != nil {
			this.reset()
			return nil, err
		}

		if this.reuseJar {
			this.jar = jar
		}
	} else {
		jar = this.jar
	}

	// release lock
	this.reset()

	redirect, err := prepareRedirect(options)
	if err != nil {
		return nil, err
	}

	c := &http.Client{
		Transport:     transport,
		CheckRedirect: redirect,
		Jar:           jar,
	}

	req, err := prepareRequest(method, url, headers, body, options)
	if err != nil {
		return nil, err
	}

	if jar != nil {
		jar.SetCookies(req.URL, cookies)
	} else {
		for _, cookie := range cookies {
			req.AddCookie(cookie)
		}
	}

	res, err := c.Do(req)

	return &Response{res}, err
}