Esempio n. 1
0
        private async ValueTask <object> PrepareHttpContextAsync(BlittableJsonReaderObject request, JsonOperationContext context, HttpContext httpContext, string method, string query, HostString host, string scheme, StringBuilder trafficWatchStringBuilder)
        {
            httpContext.Response.StatusCode = 0;
            httpContext.Request.Headers.Clear();
            httpContext.Response.Headers.Clear();
            httpContext.Request.Host        = host;
            httpContext.Request.Scheme      = scheme;
            httpContext.Request.QueryString = new QueryString(query);
            if (request.TryGet(nameof(GetRequest.Headers), out BlittableJsonReaderObject headers))
            {
                foreach (var header in headers.GetPropertyNames())
                {
                    if (headers.TryGet(header, out string value) == false)
                    {
                        continue;
                    }

                    if (string.IsNullOrWhiteSpace(value))
                    {
                        continue;
                    }

                    httpContext.Request.Headers.Add(header, value);
                }
            }
            // initiated to use it at the end of for
            object content = null;

            if (method == HttpMethod.Post.Method && request.TryGet(nameof(GetRequest.Content), out content))
            {
                if (content is LazyStringValue)
                {
                    var requestBody = GetRequestBody(content.ToString());
                    HttpContext.Response.RegisterForDispose(requestBody);
                    httpContext.Request.Body = requestBody;
                }
                else
                {
                    var requestBody = new MemoryStream();
                    await using (var contentWriter = new AsyncBlittableJsonTextWriter(context, requestBody))
                        context.Write(contentWriter, (BlittableJsonReaderObject)content);

                    HttpContext.Response.RegisterForDispose(requestBody);
                    httpContext.Request.Body          = requestBody;
                    httpContext.Request.Body.Position = 0;
                }
            }
            else if (method == HttpMethod.Get.Method && trafficWatchStringBuilder != null)
            {
                content = request.ToString();
            }

            return(content);
        }
Esempio n. 2
0
        private async Task CreateInternal(BlittableJsonReaderObject bjro, SubscriptionCreationOptions options, DocumentsOperationContext context, long?id, bool?disabled)
        {
            if (TrafficWatchManager.HasRegisteredClients)
            {
                AddStringToHttpContext(bjro.ToString(), TrafficWatchChangeType.Subscriptions);
            }

            var sub = SubscriptionConnection.ParseSubscriptionQuery(options.Query);

            if (Enum.TryParse(options.ChangeVector, out Constants.Documents.SubscriptionChangeVectorSpecialStates changeVectorSpecialValue))
            {
                switch (changeVectorSpecialValue)
                {
                case Constants.Documents.SubscriptionChangeVectorSpecialStates.BeginningOfTime:

                    options.ChangeVector = null;
                    break;

                case Constants.Documents.SubscriptionChangeVectorSpecialStates.LastDocument:
                    options.ChangeVector = Database.DocumentsStorage.GetLastDocumentChangeVector(context.Transaction.InnerTransaction, context, sub.Collection);
                    break;
                }
            }

            var mentor         = options.MentorNode;
            var subscriptionId = await Database.SubscriptionStorage.PutSubscription(options, GetRaftRequestIdFromQuery(), id, disabled, mentor);

            var name = options.Name ?? subscriptionId.ToString();

            using (ServerStore.ContextPool.AllocateOperationContext(out TransactionOperationContext serverContext))
                using (serverContext.OpenReadTransaction())
                {
                    // need to wait on the relevant remote node
                    var node = Database.SubscriptionStorage.GetResponsibleNode(serverContext, name);
                    if (node != null && node != ServerStore.NodeTag)
                    {
                        await WaitForExecutionOnSpecificNode(serverContext, ServerStore.GetClusterTopology(serverContext), node, subscriptionId);
                    }
                }

            HttpContext.Response.StatusCode = (int)HttpStatusCode.Created;

            await using (var writer = new AsyncBlittableJsonTextWriter(context, ResponseBodyStream()))
            {
                context.Write(writer, new DynamicJsonValue
                {
                    [nameof(CreateSubscriptionResult.Name)] = name
                });
            }
        }
Esempio n. 3
0
            public void WriteKeyValue(string key, BlittableJsonReaderObject value)
            {
                using (value)
                {
                    if (First == false)
                        Writer.WriteComma();
                    First = false;

                    Writer.WriteStartObject();
                    Writer.WritePropertyName("Key");
                    Writer.WriteString(key);
                    Writer.WriteComma();
                    Writer.WritePropertyName("Value");
                    Writer.WriteString(value.ToString());
                    Writer.WriteEndObject();
                }
            }
Esempio n. 4
0
        private static unsafe DynamicJsonValue ReadHistoryLog(TransactionOperationContext context, Table.TableValueHolder entryHolder)
        {
            var djv = new DynamicJsonValue();

            var ticks = Bits.SwapBytes(*(long *)entryHolder.Reader.Read((int)(LogHistoryColumn.Ticks), out _));

            djv["Date"] = new DateTime(ticks);

            int size;

            djv[nameof(LogHistoryColumn.Guid)]          = ReadGuid(entryHolder);
            djv[nameof(LogHistoryColumn.Index)]         = ReadIndex(entryHolder);
            djv[nameof(LogHistoryColumn.Term)]          = ReadTerm(entryHolder);
            djv[nameof(LogHistoryColumn.CommittedTerm)] = ReadCommittedTerm(entryHolder);
            djv[nameof(LogHistoryColumn.Type)]          = ReadType(entryHolder);
            djv[nameof(LogHistoryColumn.State)]         = ReadState(entryHolder).ToString();

            var resultPtr = entryHolder.Reader.Read((int)(LogHistoryColumn.Result), out size);

            if (size > 0)
            {
                var blittableResult = new BlittableJsonReaderObject(resultPtr, size, context);
                djv[nameof(LogHistoryColumn.Result)] = blittableResult.ToString();
                blittableResult.Dispose();
            }
            else
            {
                djv[nameof(LogHistoryColumn.Result)] = null;
            }

            var exTypePtr = entryHolder.Reader.Read((int)(LogHistoryColumn.ExceptionType), out size);

            djv[nameof(LogHistoryColumn.ExceptionType)] = size > 0 ? Encoding.UTF8.GetString(exTypePtr, size) : null;

            var exMsg = entryHolder.Reader.Read((int)(LogHistoryColumn.ExceptionMessage), out size);

            djv[nameof(LogHistoryColumn.ExceptionMessage)] = size > 0 ? Encoding.UTF8.GetString(exMsg, size) : null;

            return(djv);
        }
Esempio n. 5
0
 private static void ThrowUnexpectedMessage(string type, string expectedType, BlittableJsonReaderObject json)
 {
     if (type == "Error")
     {
         if (json.TryGet("ExceptionType", out string errorType) && errorType == typeof(TopologyMismatchException).Name)
         {
             json.TryGet("Message", out string message);
             throw new TopologyMismatchException(message);
         }
     }
     throw new InvalidDataException(
               $"Expected to get type of \'{expectedType}\' message, but got \'{type}\' message.", new Exception(json.ToString()));
 }
Esempio n. 6
0
 public override void SetResponse(JsonOperationContext context, BlittableJsonReaderObject response, bool fromCache)
 {
     // quick and dirty for the tests
     Result = JsonConvert.DeserializeObject <DocumentSize>(response.ToString());
 }
Esempio n. 7
0
 public override void SetResponse(JsonOperationContext context, BlittableJsonReaderObject response, bool fromCache)
 {
     Result = JsonConvert.DeserializeObject <T>(response.ToString());
 }