private void ExtractMultipartHeader(Encoding contentEncoding, out string partID, out string partTransferEncoding)
        {
            partID = partTransferEncoding = null;

            while (true)
            {
                var buffer = ReadLine();

                var lastLine = contentEncoding.GetString(buffer).Trim();
                if (string.IsNullOrEmpty(lastLine))
                {
                    break;
                }

                var tempContentID = XRoadHelper.ExtractValue("content-id:", lastLine, null);
                if (tempContentID != null)
                {
                    partID = tempContentID.Trim().Trim('<', '>');
                }

                var tempTransferEncoding = XRoadHelper.ExtractValue("content-transfer-encoding:", lastLine, null);
                if (tempTransferEncoding != null)
                {
                    partTransferEncoding = tempTransferEncoding;
                }
            }
        }
        /// <summary>
        /// Handle incoming request.
        /// </summary>
        public virtual void ProcessRequest(HttpContext httpContext)
        {
            using (var context = new XRoadContext(httpContext))
            {
                try
                {
                    context.MessageFormatter = XRoadHelper.GetMessageFormatter(httpContext.Request.ContentType);

                    httpContext.Request.InputStream.Position = 0;
                    httpContext.Response.ContentType         = XRoadHelper.GetContentTypeHeader(context.MessageFormatter.ContentType);

                    HandleRequest(context);
                }
                catch (Exception exception)
                {
                    if (context.MessageFormatter == null)
                    {
                        context.MessageFormatter = new SoapMessageFormatter();
                    }

                    httpContext.Response.ContentType = XRoadHelper.GetContentTypeHeader(context.MessageFormatter.ContentType);

                    if (httpContext.Response.OutputStream.CanSeek)
                    {
                        httpContext.Response.OutputStream.Seek(0, SeekOrigin.Begin);
                        httpContext.Response.OutputStream.SetLength(0);
                    }

                    var fault = context.MessageFormatter.CreateFault(exception);

                    OnExceptionOccurred(context, exception, fault);
                }
            }
        }
        public static async Task Invoke(HttpContext httpContext, IWebServiceHandler handler)
        {
            var options  = httpContext.RequestServices.GetRequiredService <XRoadLibOptions>();
            var accessor = httpContext.RequestServices.GetRequiredService <IWebServiceContextAccessor>();

            if (handler is WebServiceRequestHandler requestHandler)
            {
                requestHandler.StoragePath = options.StoragePath;
            }

            using (var context = new WebServiceContext(httpContext))
            {
                if (accessor is WebServiceContextAccessor webServiceContextAccessor)
                {
                    webServiceContextAccessor.WebServiceContext = context;
                }

                try
                {
                    context.MessageFormatter = XRoadHelper.GetMessageFormatter(context.HttpContext.Request.ContentType);

                    if (httpContext.Request.Body.CanSeek)
                    {
                        httpContext.Request.Body.Position = 0;
                    }

                    httpContext.Response.ContentType = XRoadHelper.GetContentTypeHeader(context.MessageFormatter.ContentType);

                    await handler.HandleRequestAsync(context);
                }
                catch (Exception exception)
                {
                    if (context.MessageFormatter == null)
                    {
                        context.MessageFormatter = new SoapMessageFormatter();
                    }

                    httpContext.Response.ContentType = XRoadHelper.GetContentTypeHeader(context.MessageFormatter.ContentType);

                    if (httpContext.Response.Body.CanSeek)
                    {
                        httpContext.Response.Body.Seek(0, SeekOrigin.Begin);
                        httpContext.Response.Body.SetLength(0);
                    }

                    var fault = context.MessageFormatter.CreateFault(exception);

                    await handler.HandleExceptionAsync(context, exception, fault);
                }
            }
        }
        public void Read(XRoadMessage target, bool isResponse = false)
        {
            target.IsMultipartContainer = XRoadHelper.IsMultipartMsg(contentTypeHeader);
            if (target.IsMultipartContainer)
            {
                target.MultipartContentType = XRoadHelper.GetMultipartContentType(contentTypeHeader);
            }

            if (stream.CanSeek && stream.Length == 0)
            {
                return;
            }

            if (stream.CanSeek)
            {
                stream.Position = 0;
            }

            streamPosition = 0;

            target.ContentEncoding = GetContentEncoding();
            target.ContentStream   = new MemoryStream();

            ReadMessageParts(target);

            target.ContentStream.Position = 0;

            using (var reader = XmlReader.Create(target.ContentStream, new XmlReaderSettings {
                CloseInput = false
            }))
            {
                var serviceManager = DetectServiceManager(reader, target);
                ParseXRoadHeader(target, reader, serviceManager);

                target.RootElementName = ParseMessageRootElementName(reader);

                if (target.ServiceManager == null && target.RootElementName != null)
                {
                    target.ServiceManager = serviceManagers.SingleOrDefault(p => p.IsHeaderNamespace(target.RootElementName.NamespaceName));
                }

                target.MetaServiceMap = GetMetaServiceMap(target.RootElementName);
            }

            if (target.Header is IXRoadHeader40 xrh4 && xrh4.ProtocolVersion?.Trim() != "4.0")
            {
                throw new InvalidQueryException($"Unsupported X-Road v6 protocol version value `{xrh4.ProtocolVersion ?? string.Empty}`.");
            }

            if (target.IsMultipartContainer)
            {
                target.BinaryMode = BinaryMode.Attachment;
            }

            if (ContentTypes.XOP.Equals(target.MultipartContentType))
            {
                target.BinaryMode = BinaryMode.Xml;
            }

            if (isResponse)
            {
                return;
            }

            var serviceCode = (target.Header as IXRoadHeader)?.Service?.ServiceCode;

            if (target.RootElementName == null || string.IsNullOrWhiteSpace(serviceCode))
            {
                return;
            }

            if (!Equals(target.RootElementName.LocalName, serviceCode))
            {
                throw new InvalidQueryException($"X-Road operation name `{serviceCode}` does not match request wrapper element name `{target.RootElementName}`.");
            }
        }
 private string GetMultipartBoundary()
 {
     return(XRoadHelper.ExtractValue("boundary=", contentTypeHeader, ";")?.Trim().Trim('"'));
 }
 private string GetMultipartStartContentID()
 {
     return(XRoadHelper.ExtractValue("start=", contentTypeHeader, ";")?.Trim().Trim('"'));
 }
        private Encoding GetContentEncoding()
        {
            var contentType = XRoadHelper.ExtractValue("charset=", contentTypeHeader, ";")?.Trim().Trim('"');

            return(string.IsNullOrWhiteSpace(contentType) || contentType.ToUpper().Equals("UTF-8") ? XRoadEncoding.Utf8 : Encoding.GetEncoding(contentType));
        }