示例#1
0
func getPrepared(a httpRequestArgs) (*plan.Prepared, errors.Error) {
	prepared_field, err := a.getValue(PREPARED)
	if err != nil || prepared_field == nil {
		return nil, err
	}

	prepared, e := plan.PreparedCache().GetPrepared(prepared_field)
	if e != nil {
		return nil, errors.NewServiceErrorBadValue(e, PREPARED)
	}
	if prepared != nil {
		return prepared, nil
	}

	prepared = &plan.Prepared{}
	json_bytes, e := prepared_field.MarshalJSON()
	if e != nil {
		return nil, errors.NewServiceErrorBadValue(e, PREPARED)
	}

	e = prepared.UnmarshalJSON(json_bytes)
	if e != nil {
		return nil, errors.NewServiceErrorBadValue(e, PREPARED)
	}

	e = plan.PreparedCache().AddPrepared(prepared)
	if e != nil {
		return nil, errors.NewServiceErrorBadValue(e, PREPARED)
	}

	return prepared, nil
}
示例#2
0
func getCredentials(a httpRequestArgs,
	hdrCreds *url.Userinfo, auths []string) (datastore.Credentials, errors.Error) {
	var creds datastore.Credentials

	if hdrCreds != nil {
		// Credentials are in the request URL:
		username := hdrCreds.Username()
		password, _ := hdrCreds.Password()
		creds = make(datastore.Credentials)
		creds[username] = password
		return creds, nil
	}
	if len(auths) > 0 {
		// Credentials are in the request header:
		// TODO: implement non-Basic auth (digest, ntlm)
		auth := auths[0]
		if strings.HasPrefix(auth, "Basic ") {
			encoded_creds := strings.Split(auth, " ")[1]
			decoded_creds, err := base64.StdEncoding.DecodeString(encoded_creds)
			if err != nil {
				return creds, errors.NewServiceErrorBadValue(err, CREDS)
			}
			// Authorization header is in format "user:pass"
			// per http://tools.ietf.org/html/rfc1945#section-10.2
			u_details := strings.Split(string(decoded_creds), ":")
			switch len(u_details) {
			case 2:
				creds = make(datastore.Credentials)
				creds[u_details[0]] = u_details[1]
			case 3:
				creds = make(datastore.Credentials)
				// Support usernames like "local:xxx" or "admin:xxx"
				creds[strings.Join(u_details[:2], ":")] = u_details[2]
			default:
				// Authorization header format is incorrect
				return creds, errors.NewServiceErrorBadValue(nil, CREDS)
			}
		}
		return creds, nil
	}
	// Credentials may be in request arguments:
	cred_data, err := a.getCredentials()
	if err == nil && len(cred_data) > 0 {
		creds = make(datastore.Credentials)
		for _, cred := range cred_data {
			user, user_ok := cred["user"]
			pass, pass_ok := cred["pass"]
			if user_ok && pass_ok {
				creds[user] = pass
			} else {
				err = errors.NewServiceErrorMissingValue("user or pass")
				break
			}
		}
	}
	return creds, err
}
示例#3
0
// helper function to create a time.Duration instance from a given string.
// There must be a unit - valid units are "ns", "us", "ms", "s", "m", "h"
func newDuration(s string) (duration time.Duration, err errors.Error) {
	// Error if given string has no unit
	last_char := s[len(s)-1]
	if last_char != 's' && last_char != 'm' && last_char != 'h' {
		err = errors.NewServiceErrorBadValue(nil,
			fmt.Sprintf("duration value %s: missing or incorrect unit "+
				"(valid units: ns, us, ms, s, m, h)", s))
	}
	if err == nil {
		d, e := time.ParseDuration(s)
		if e != nil {
			err = errors.NewServiceErrorBadValue(e, "duration")
		} else {
			duration = d
		}
	}
	return
}
示例#4
0
// create a jsonArgs structure from the given http request.
func newJsonArgs(req *http.Request) (*jsonArgs, errors.Error) {
	var p jsonArgs
	decoder := json.NewDecoder(req.Body)
	err := decoder.Decode(&p.args)
	if err != nil {
		return nil, errors.NewServiceErrorBadValue(err, "JSON request body")
	}
	p.req = req
	return &p, nil
}
示例#5
0
func (this *urlArgs) getCredentials() ([]map[string]string, errors.Error) {
	var creds_data []map[string]string

	creds_field, err := this.formValue(CREDS)
	if err == nil && creds_field != "" {
		decoder := json.NewDecoder(strings.NewReader(creds_field))
		e := decoder.Decode(&creds_data)
		if e != nil {
			err = errors.NewServiceErrorBadValue(e, CREDS)
		}
	}
	return creds_data, err
}
示例#6
0
func (this *urlArgs) getTristate(f string) (value.Tristate, errors.Error) {
	tristate_value := value.NONE
	value_field, err := this.formValue(f)
	if err != nil {
		return tristate_value, err
	}
	if value_field == "" {
		return tristate_value, nil
	}
	bool_value, e := strconv.ParseBool(value_field)
	if e != nil {
		return tristate_value, errors.NewServiceErrorBadValue(e, f)
	}
	tristate_value = value.ToTristate(bool_value)
	return tristate_value, nil
}
示例#7
0
func (this *urlArgs) getStatement() (string, errors.Error) {
	statement, err := this.formValue(STATEMENT)
	if err != nil {
		return "", err
	}

	if statement == "" && this.req.Method == "POST" {
		bytes, err := ioutil.ReadAll(this.req.Body)
		if err != nil {
			return "", errors.NewServiceErrorBadValue(err, STATEMENT)
		}

		statement = string(bytes)
	}

	return statement, nil
}
示例#8
0
func (this *jsonArgs) getScanVector() (timestamp.Vector, errors.Error) {
	var type_ok bool

	scan_vector_data_field, in_request := this.args[SCAN_VECTOR]
	if !in_request {
		return nil, nil
	}
	full_vector_data, type_ok := scan_vector_data_field.([]interface{})
	if type_ok {
		if len(full_vector_data) != SCAN_VECTOR_SIZE {
			return nil, errors.NewServiceErrorTypeMismatch(SCAN_VECTOR,
				fmt.Sprintf("array of %d entries", SCAN_VECTOR_SIZE))
		}
		entries := make([]timestamp.Entry, len(full_vector_data))
		for index, arg := range full_vector_data {
			nextEntry, err := makeVectorEntry(index, arg)
			if err != nil {
				return nil, err
			}
			entries[index] = nextEntry
		}
	}
	sparse_vector_data, type_ok := scan_vector_data_field.(map[string]interface{})
	if !type_ok {
		return nil, errors.NewServiceErrorTypeMismatch(SCAN_VECTOR, "array or map of { number, string }")
	}
	entries := make([]timestamp.Entry, len(sparse_vector_data))
	i := 0
	for key, arg := range sparse_vector_data {
		index, e := strconv.Atoi(key)
		if e != nil {
			return nil, errors.NewServiceErrorBadValue(e, SCAN_VECTOR)
		}
		nextEntry, err := makeVectorEntry(index, arg)
		if err != nil {
			return nil, err
		}
		entries[i] = nextEntry
		i = i + 1
	}
	return &scanVectorEntries{
		entries: entries,
	}, nil
}
示例#9
0
// makeSparseVector is used when the request contains a sparse entry arg
func makeSparseVector(args map[string]*restArg) (*scanVectorEntries, errors.Error) {
	entries := make([]timestamp.Entry, len(args))
	i := 0
	for key, arg := range args {
		index, err := strconv.Atoi(key)
		if err != nil {
			return nil, errors.NewServiceErrorBadValue(err, SCAN_VECTOR)
		}
		entries[i] = &scanVectorEntry{
			pos:  uint32(index),
			val:  arg.Seqno,
			uuid: arg.Uuid,
		}
		i = i + 1
	}
	return &scanVectorEntries{
		entries: entries,
	}, nil
}
示例#10
0
func (this *urlArgs) getScanVector() (timestamp.Vector, errors.Error) {
	var full_vector_data []*restArg
	var sparse_vector_data map[string]*restArg

	scan_vector_data_field, err := this.formValue(SCAN_VECTOR)

	if err != nil || scan_vector_data_field == "" {
		return nil, err
	}
	decoder := json.NewDecoder(strings.NewReader(scan_vector_data_field))
	e := decoder.Decode(&full_vector_data)
	if e == nil {
		return makeFullVector(full_vector_data)
	}
	decoder = json.NewDecoder(strings.NewReader(scan_vector_data_field))
	e = decoder.Decode(&sparse_vector_data)
	if e != nil {
		return nil, errors.NewServiceErrorBadValue(e, SCAN_VECTOR)
	}
	return makeSparseVector(sparse_vector_data)
}
示例#11
0
// Positional args are of the form: args=json_list
func (this *urlArgs) getPositionalArgs() (value.Values, errors.Error) {
	var positionalArgs value.Values

	args_field, err := this.formValue(ARGS)
	if err != nil || args_field == "" {
		return positionalArgs, err
	}

	var args []interface{}

	decoder := json.NewDecoder(strings.NewReader(args_field))
	e := decoder.Decode(&args)
	if e != nil {
		return positionalArgs, errors.NewServiceErrorBadValue(e, ARGS)
	}

	positionalArgs = make([]value.Value, len(args))
	// Put each element of args into positionalArgs
	for i, arg := range args {
		positionalArgs[i] = value.NewValue(arg)
	}

	return positionalArgs, nil
}
示例#12
0
func newHttpRequest(resp http.ResponseWriter, req *http.Request, bp BufferPool) *httpRequest {
	var httpArgs httpRequestArgs
	var err errors.Error

	e := req.ParseForm()
	if e != nil {
		err = errors.NewServiceErrorBadValue(e, "request form")
	}

	if err != nil && req.Method != "GET" && req.Method != "POST" {
		err = errors.NewServiceErrorHTTPMethod(req.Method)
	}

	if err == nil {
		httpArgs, err = getRequestParams(req)
	}

	var statement string
	if err == nil {
		statement, err = httpArgs.getStatement()
	}

	var prepared *plan.Prepared
	if err == nil {
		prepared, err = getPrepared(httpArgs)
	}

	if err == nil && statement == "" && prepared == nil {
		err = errors.NewServiceErrorMissingValue("statement or prepared")
	}

	var namedArgs map[string]value.Value
	if err == nil {
		namedArgs, err = httpArgs.getNamedArgs()
	}

	var positionalArgs value.Values
	if err == nil {
		positionalArgs, err = httpArgs.getPositionalArgs()
	}

	var namespace string
	if err == nil {
		namespace, err = httpArgs.getString(NAMESPACE, "")
	}

	var timeout time.Duration
	if err == nil {
		timeout, err = httpArgs.getDuration(TIMEOUT)
	}

	var readonly value.Tristate
	if err == nil {
		readonly, err = httpArgs.getTristate(READONLY)
	}
	if err == nil && readonly == value.FALSE && req.Method == "GET" {
		err = errors.NewServiceErrorReadonly(
			fmt.Sprintf("%s=false cannot be used with HTTP GET method.", READONLY))
	}

	var metrics value.Tristate
	if err == nil {
		metrics, err = httpArgs.getTristate(METRICS)
	}

	var format Format
	if err == nil {
		format, err = getFormat(httpArgs)
	}

	if err == nil && format != JSON {
		err = errors.NewServiceErrorNotImplemented("format", format.String())
	}

	var signature value.Tristate
	if err == nil {
		signature, err = httpArgs.getTristate(SIGNATURE)
	}

	var compression Compression
	if err == nil {
		compression, err = getCompression(httpArgs)
	}

	if err == nil && compression != NONE {
		err = errors.NewServiceErrorNotImplemented("compression", compression.String())
	}

	var encoding Encoding
	if err == nil {
		encoding, err = getEncoding(httpArgs)
	}

	if err == nil && encoding != UTF8 {
		err = errors.NewServiceErrorNotImplemented("encoding", encoding.String())
	}

	var pretty value.Tristate
	if err == nil {
		pretty, err = httpArgs.getTristate(PRETTY)
	}

	if err == nil && pretty == value.FALSE {
		err = errors.NewServiceErrorNotImplemented("pretty", "false")
	}

	var consistency *scanConfigImpl

	if err == nil {
		consistency, err = getScanConfiguration(httpArgs)
	}

	var creds datastore.Credentials
	if err == nil {
		creds, err = getCredentials(httpArgs, req.URL.User, req.Header["Authorization"])
	}

	client_id := ""
	if err == nil {
		client_id, err = httpArgs.getString(CLIENT_CONTEXT_ID, "")
	}

	base := server.NewBaseRequest(statement, prepared, namedArgs, positionalArgs,
		namespace, readonly, metrics, signature, consistency, client_id, creds)

	rv := &httpRequest{
		BaseRequest: *base,
		resp:        resp,
		req:         req,
	}

	rv.SetTimeout(rv, timeout)

	rv.writer = NewBufferedWriter(rv, bp)

	// Limit body size in case of denial-of-service attack
	req.Body = http.MaxBytesReader(resp, req.Body, MAX_REQUEST_BYTES)

	// Abort if client closes connection
	closeNotify := resp.(http.CloseNotifier).CloseNotify()
	go func() {
		<-closeNotify
		rv.Stop(server.TIMEOUT)
	}()

	if err != nil {
		rv.Fail(err)
	}

	return rv
}