Esempio n. 1
0
// Get stream as string
//
// @param  firstLine string
// @return string
// @protected
func (this *Stream) toString(firstLine string) string {
	str := firstLine

	// add headers
	if this.Headers != nil {
		for key, value := range this.Headers {
			if key == "0" { // response only
				continue
			}
			if value != nil { // remove?
				str += util.StringFormat("%s: %s\r\n", key, value)
			}
		}
	}

	// add headers/body seperator
	str += "\r\n"

	// add body
	if this.Body != nil {
		switch this.Body.(type) {
		case string:
			str += this.Body.(string)
		default:
			body, err := util.UnparseBody(this.Body)
			if err == nil {
				str += body
			}
		}
	}

	return str
}
Esempio n. 2
0
// Ping.
//
// @param  statusCodes... uint16
// @return bool
// @panics
func (this *DocumentAttachment) Ping(statusCodes ...uint16) bool {
	if this.Document == nil {
		panic("Attachment document is not defined!")
	}

	docId := this.Document.GetId()
	docRev := this.Document.GetRev()
	if docId == "" {
		panic("Attachment document _id is required!")
	}
	if this.FileName == "" {
		panic("Attachment file name is required!")
	}

	query, headers := util.Map(), util.Map()
	if docRev != "" {
		query["rev"] = docRev
	}
	if this.Digest != "" {
		headers["If-None-Match"] = util.Quote(this.Digest)
	}

	database := this.Document.GetDatabase()
	response := database.Client.Head(util.StringFormat("%s/%s/%s",
		database.Name, docId, util.UrlEncode(this.FileName)), query, headers)

	// try to match given status codes
	for _, statusCode := range statusCodes {
		if response.GetStatusCode() == statusCode {
			return true
		}
	}

	return false
}
Esempio n. 3
0
// Copy to.
//
// @param  dest   string
// @param  destRev string
// @param  args... bool
// @return map[string]interface{}, error
// @panics
func (this *Document) CopyTo(dest, destRev string, args ...bool) (map[string]interface{}, error) {
	id, rev := this.GetId(), this.GetRev()
	if id == "" || rev == "" {
		panic("Both _id & _rev fields could not be empty!")
	}
	if dest == "" || destRev == "" {
		panic("Destination & destination revision could not be empty!")
	}

	query, headers := util.Map(), util.Map()
	headers["If-Match"] = rev
	headers["Destination"] = util.StringFormat("%s?rev=%s", dest, destRev)

	if args != nil {
		if args[0] == true {
			query["batch"] = "ok"
		}
		if args[1] == true {
			headers["X-Couch-Full-Commit"] = "true"
		}
	}

	data, err := this.Database.Client.Copy(this.Database.Name+"/"+util.UrlEncode(id), query, headers).
		GetBodyData(nil)
	if err != nil {
		return nil, err
	}

	return map[string]interface{}{
		"ok":  util.DigBool("ok", data),
		"id":  util.DigString("id", data),
		"rev": util.DigString("rev", data),
	}, nil
}
Esempio n. 4
0
// Find.
//
// @return map[string]interface{}
// @panics
func (this *DocumentAttachment) Find() map[string]interface{} {
	if this.Document == nil {
		panic("Attachment document is not defined!")
	}

	docId := this.Document.GetId()
	docRev := this.Document.GetRev()
	if docId == "" {
		panic("Attachment document _id is required!")
	}
	if this.FileName == "" {
		panic("Attachment file name is required!")
	}

	query, headers := util.Map(), util.Map()
	if docRev != "" {
		query["rev"] = docRev
	}
	if this.Digest != "" {
		headers["If-None-Match"] = util.Quote(this.Digest)
	}
	headers["Accept"] = "*/*"
	headers["Content-Type"] = nil // nil=remove

	database := this.Document.GetDatabase()
	response := database.Client.Get(util.StringFormat("%s/%s/%s",
		database.Name, docId, util.UrlEncode(this.FileName)), query, headers)
	statusCode := response.GetStatusCode()

	ret := util.Map()
	// try to match excepted status code
	if statusCode == 200 || statusCode == 304 {
		ret["content"] = response.GetBody()
		ret["content_type"] = response.GetHeader("Content-Type")
		ret["content_length"] = util.UInt(response.GetHeader("Content-Length"))
		// set digest
		md5 := response.GetHeader("Content-MD5")
		if md5 == nil {
			md5 = response.GetHeader("ETag")
		}
		ret["digest"] = "md5-" + util.Trim(md5.(string), "\"")
	}

	return ret
}
Esempio n. 5
0
// Remove.
//
// @return map[string]interface{}, error
// @panics
func (this *DocumentAttachment) Remove(args ...bool) (map[string]interface{}, error) {
	if this.Document == nil {
		panic("Attachment document is not defined!")
	}

	docId := this.Document.GetId()
	docRev := this.Document.GetRev()
	if docId == "" {
		panic("Attachment document _id is required!")
	}
	if docRev == "" {
		panic("Attachment document _rev is required!")
	}
	if this.FileName == "" {
		panic("Attachment file name is required!")
	}

	query, headers := util.Map(), util.Map()
	if args != nil {
		if args[0] {
			query["batch"] = "ok"
		}
		if args[1] {
			headers["X-Couch-Full-Commit"] = "true"
		}
	}
	headers["If-Match"] = docRev

	data, err := this.Document.Database.Client.Delete(util.StringFormat(
		"%s/%s/%s", this.Document.Database.Name, docId, util.UrlEncode(this.FileName),
	), nil, headers,
	).GetBodyData(nil)
	if err != nil {
		return nil, err
	}

	return map[string]interface{}{
		"ok":  util.DigBool("ok", data),
		"id":  util.DigString("id", data),
		"rev": util.DigString("rev", data),
	}, nil
}
Esempio n. 6
0
// Save.
//
// @return map[string]interface{}, error
// @panics
func (this *DocumentAttachment) Save() (map[string]interface{}, error) {
	if this.Document == nil {
		panic("Attachment document is not defined!")
	}

	docId := this.Document.GetId()
	docRev := this.Document.GetRev()
	if docId == "" {
		panic("Attachment document _id is required!")
	}
	if docRev == "" {
		panic("Attachment document _rev is required!")
	}
	if this.FileName == "" {
		panic("Attachment file name is required!")
	}

	// read file contents
	this.ReadFile(false)

	headers := util.Map()
	headers["If-Match"] = docRev
	headers["Content-Type"] = this.ContentType

	data, err := this.Document.Database.Client.Put(util.StringFormat(
		"%s/%s/%s", this.Document.Database.Name, docId, util.UrlEncode(this.FileName),
	), nil, this.Data, headers,
	).GetBodyData(nil)
	if err != nil {
		return nil, err
	}

	return map[string]interface{}{
		"ok":  util.DigBool("ok", data),
		"id":  util.DigString("id", data),
		"rev": util.DigString("rev", data),
	}, nil
}
Esempio n. 7
0
// Get request as string.
//
// @return string
// @implemented
func (this *Request) ToString() string {
	return this.toString(util.StringFormat(
		"%s %s HTTP/%s\r\n", this.Method, this.Uri, this.HttpVersion,
	))
}
Esempio n. 8
0
// Get response as string.
//
// @return string
// @implemented
func (this *Response) ToString() string {
	return this.toString(util.StringFormat(
		"HTTP/%s %d %s\r\n", this.HttpVersion, this.StatusCode, this.StatusText,
	))
}