コード例 #1
0
ファイル: service_request.go プロジェクト: jmptrader/query
func getScanConfiguration(a httpRequestArgs) (*scanConfigImpl, errors.Error) {

	scan_consistency_field, err := a.getString(SCAN_CONSISTENCY, "NOT_BOUNDED")
	if err != nil {
		return nil, err
	}

	scan_level := newScanConsistency(scan_consistency_field)
	if scan_level == server.UNDEFINED_CONSISTENCY {
		return nil, errors.NewServiceErrorUnrecognizedValue(SCAN_CONSISTENCY, scan_consistency_field)
	}

	scan_wait, err := a.getDuration(SCAN_WAIT)
	if err != nil {
		return nil, err
	}

	scan_vector, err := a.getScanVector()
	if err != nil {
		return nil, err
	}

	if scan_level == server.AT_PLUS && scan_vector == nil {
		return nil, errors.NewServiceErrorMissingValue(SCAN_VECTOR)
	}

	return &scanConfigImpl{
		scan_level:  scan_level,
		scan_wait:   scan_wait,
		scan_vector: scan_vector,
	}, nil
}
コード例 #2
0
ファイル: service_request.go プロジェクト: pkdevboxy/query
func getScanConfiguration(a httpRequestArgs) (*scanConfigImpl, errors.Error) {

	scan_consistency_field, err := a.getString(SCAN_CONSISTENCY, "NOT_BOUNDED")
	if err != nil {
		return nil, err
	}

	scan_level := newScanConsistency(scan_consistency_field)
	if scan_level == server.UNDEFINED_CONSISTENCY {
		return nil, errors.NewServiceErrorUnrecognizedValue(SCAN_CONSISTENCY, scan_consistency_field)
	}

	scan_wait, err := a.getDuration(SCAN_WAIT)
	if err != nil {
		return nil, err
	}

	scan_vector, err := a.getScanVector()
	if err != nil {
		return nil, err
	}

	scan_vectors, err := a.getScanVectors()
	if err != nil {
		return nil, err
	}

	var scan_vector_source timestamp.ScanVectorSource
	if scan_vector == nil {
		if scan_vectors == nil {
			if scan_level == server.AT_PLUS && scan_vector == nil && scan_vectors == nil {
				return nil, errors.NewServiceErrorMissingValue(SCAN_VECTOR)
			}
			scan_vector_source = &ZeroScanVectorSource{}
		} else {
			defaultNamespace, err := a.getString(NAMESPACE, "default")
			if err != nil {
				return nil, err
			}
			scan_vector_source = newMultipleScanVectorSource(defaultNamespace, scan_vectors)
		}
	} else {
		if scan_vectors == nil {
			scan_vector_source = &singleScanVectorSource{scan_vector: scan_vector}
		} else {
			// Not both scan_vector and scan_vectors.
			return nil, errors.NewServiceErrorMultipleValues("scan_vector and scan_vectors")
		}
	}

	return &scanConfigImpl{
		scan_level:         scan_level,
		scan_wait:          scan_wait,
		scan_vector_source: scan_vector_source,
	}, nil
}
コード例 #3
0
ファイル: service_request.go プロジェクト: pkdevboxy/query
func getCredentials(a httpRequestArgs, auths []string) (datastore.Credentials, errors.Error) {
	cred_data, err := a.getCredentials()
	if err != nil {
		return nil, err
	}

	if len(cred_data) > 0 {
		// Credentials are in request parameters:
		creds := 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, nil
	}

	if len(auths) > 0 {
		// Credentials are in http header:
		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 nil, 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), ":")
			creds := datastore.Credentials{}
			switch len(u_details) {
			case 2:
				creds[u_details[0]] = u_details[1]
			case 3:
				// Support usernames like "local:xxx" or "admin:xxx"
				creds[strings.Join(u_details[:2], ":")] = u_details[2]
			default:
				// Authorization header format is incorrect
				return nil, errors.NewServiceErrorBadValue(nil, CREDS)
			}
			return creds, nil
		}
	}

	return nil, err
}
コード例 #4
0
ファイル: service_request.go プロジェクト: pkdevboxy/query
// A named argument is an argument of the form: $<identifier>=json_value
func (this *urlArgs) getNamedArgs() (map[string]value.Value, errors.Error) {
	var args map[string]value.Value

	for name, _ := range this.req.Form {
		if !strings.HasPrefix(name, "$") {
			continue
		}
		arg, err := this.formValue(name)
		if err != nil {
			return args, err
		}
		if len(arg) == 0 {
			//This is an error - there _has_ to be a value for a named argument
			return args, errors.NewServiceErrorMissingValue(fmt.Sprintf("named argument %s", name))
		}
		args = addNamedArg(args, name, value.NewValue([]byte(arg)))
	}
	return args, nil
}
コード例 #5
0
ファイル: service_request.go プロジェクト: pkdevboxy/query
func newHttpRequest(resp http.ResponseWriter, req *http.Request, bp BufferPool, size int) *httpRequest {
	var httpArgs httpRequestArgs
	var err errors.Error

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

	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)
	}

	err = contentNegotiation(resp, req)

	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)
		plan, plan_err := getEncodedPlan(httpArgs)
		if err != nil && err.Code() == errors.NO_SUCH_PREPARED {
			if plan_err != nil {
				err = plan_err
			}
			if plan_err == nil && plan != nil {
				prepared = plan
				err = nil
			}
		}
		if err == nil && plan_err != nil {
			err = plan_err
		}
		if prepared != nil && plan != nil && prepared.EncodedPlan() != plan.EncodedPlan() {
			err = errors.NewPreparedEncodingMismatchError(prepared.Name())
		}
	}

	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 max_parallelism int
	if err == nil {
		var maxp string
		maxp, err = httpArgs.getString(MAX_PARALLELISM, "")
		if err == nil && maxp != "" {
			var e error
			max_parallelism, e = strconv.Atoi(maxp)
			if e != nil {
				err = errors.NewServiceErrorBadValue(e, "max parallelism")
			}
		}
	}

	var readonly value.Tristate
	if err == nil {
		readonly, err = getReadonly(httpArgs, req.Method == "GET")
	}

	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.Header["Authorization"])
	}

	client_id := ""
	if err == nil {
		client_id, err = getClientID(httpArgs)
	}

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

	rv := &httpRequest{
		BaseRequest:   *base,
		resp:          resp,
		req:           req,
		requestNotify: make(chan bool, 1),
	}

	rv.SetTimeout(rv, timeout)

	rv.writer = NewBufferedWriter(rv, bp)

	// Abort if client closes connection; alternatively, return when request completes.
	closeNotify := resp.(http.CloseNotifier).CloseNotify()
	closeNotifier := func() {
		select {
		case <-closeNotify:
			rv.Expire()
			return
		case <-rv.requestNotify:
			return
		}
	}
	go closeNotifier()

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

	return rv
}