Beispiel #1
0
            public Stream VisitByteArrayBody(ByteArrayHttpBody body)
            {
                var result = new MemoryStream(body.Data);

                result.Position = 0;
                return(result);
            }
        public override Task ApplyArgument(HttpApiRequest request, string name, object argument)
        {
            if (request.Body != null)
                throw new Exception("Can only use ByteArrayArgumentHandler for one argument.  If you need multiple arguments, you " +
                                    "should be using MultipartStreamArgumentHandler, which ought to have been selected " +
                                    "automatically under default conditions.");

            var streamBody = new ByteArrayHttpBody((byte[])argument);
            request.Body = streamBody;

            return base.ApplyArgument(request, name, argument);
        }
Beispiel #3
0
        public override Task ApplyArgument(HttpApiRequest request, string name, object argument)
        {
            if (request.Body != null)
            {
                throw new Exception("Can only use ByteArrayArgumentHandler for one argument.  If you need multiple arguments, you " +
                                    "should be using MultipartStreamArgumentHandler, which ought to have been selected " +
                                    "automatically under default conditions.");
            }

            var streamBody = new ByteArrayHttpBody((byte[])argument);

            request.Body = streamBody;

            return(base.ApplyArgument(request, name, argument));
        }
Beispiel #4
0
        public static MultipartHttpBody ParseMultipart(HttpListenerRequest request)
        {
            var extraData = request.ContentType
                .Split(';')
                .Skip(1)
                .Select(x => x.Trim().Split('='))
                .ToDictionary(x => x[0], x => x[1].StartsWith("\"") ? x[1].Substring(1, x[1].Length - 2) : x[1]);

            var boundary = "--" + extraData["boundary"];
            var input = new MemoryStream();
            var position = 0L;
            request.InputStream.CopyTo(input);
            var buffer = input.ToArray();
            var result = new MultipartHttpBody();

            //            var s2 = new StreamReader(new MemoryStream(buffer)).ReadToEnd();
            var boundaryBytes = Encoding.UTF8.GetBytes(boundary);

            var boundaryLine = ReadLine(buffer, ref position);
            if (boundaryLine != boundary)
                throw new Exception($"Expected boundary but found: {boundaryLine}");

            while (true)
            {
                string name = null;
                string fileName = null;
                string contentType = null;
                for (var line = ReadLine(buffer, ref position); line != ""; line = ReadLine(buffer, ref position))
                {
                    int colonIndex = line.IndexOf(':');
                    var headerName = line.Substring(0, colonIndex);
                    var headerValue = line.Substring(colonIndex + 2);

                    switch (headerName)
                    {
                        case "Content-Disposition":
                        {
                            var header = ContentDispositionHeaderValue.Parse(headerValue);
                            name = header.Name;
                            fileName = header.FileName;
                            break;
                        }
                        case "Content-Type":
                        {
                            var header = MediaTypeHeaderValue.Parse(headerValue);
                            contentType = header.MediaType;
                            break;
                        }
                    }
                }

                var endBoundary = IndexOf(buffer, position, boundaryBytes);
                var dataBuffer = ReadBuffer(buffer, ref position, endBoundary - position);
                Func<string> getText = () =>
                {
                    var s = Encoding.UTF8.GetString(dataBuffer);
            //                    s = s.Substring(0, s.Length - 2);
                    return s;
                };
                HttpBody body;
                switch (contentType)
                {
                    case "text/plain":
                        body = new StringHttpBody(getText());
                        break;
                    case "application/json":
                        body = new JsonHttpBody(JToken.Parse(getText()));
                        break;
                    case "application/octet-stream":
                        body = new ByteArrayHttpBody { Data = dataBuffer };
                        break;
                    default:
                        throw new Exception($"Unsupported media type: {contentType}");
                }
                result.Data[name] = new MultipartData { FileName = fileName, Body = body };

                var endBoundaryLine = ReadLine(buffer, ref position);
                if (endBoundaryLine == boundaryLine + "--")
                    break;
                if (endBoundaryLine != boundaryLine)
                    throw new Exception($"Expected ending boundary but found: {boundaryLine}");
            }
            return result;
        }
Beispiel #5
0
        public static MultipartHttpBody ParseMultipart(HttpListenerRequest request)
        {
            var extraData = request.ContentType
                            .Split(';')
                            .Skip(1)
                            .Select(x => x.Trim().Split('='))
                            .ToDictionary(x => x[0], x => x[1].StartsWith("\"") ? x[1].Substring(1, x[1].Length - 2) : x[1]);

            var boundary = "--" + extraData["boundary"];
            var input    = new MemoryStream();
            var position = 0L;

            request.InputStream.CopyTo(input);
            var buffer = input.ToArray();
            var result = new MultipartHttpBody();

//            var s2 = new StreamReader(new MemoryStream(buffer)).ReadToEnd();
            var boundaryBytes = Encoding.UTF8.GetBytes(boundary);

            var boundaryLine = ReadLine(buffer, ref position);

            if (boundaryLine != boundary)
            {
                throw new Exception($"Expected boundary but found: {boundaryLine}");
            }

            while (true)
            {
                string name        = null;
                string fileName    = null;
                string contentType = null;
                for (var line = ReadLine(buffer, ref position); line != ""; line = ReadLine(buffer, ref position))
                {
                    int colonIndex  = line.IndexOf(':');
                    var headerName  = line.Substring(0, colonIndex);
                    var headerValue = line.Substring(colonIndex + 2);

                    switch (headerName)
                    {
                    case "Content-Disposition":
                    {
                        var header = ContentDispositionHeaderValue.Parse(headerValue);
                        name     = header.Name;
                        fileName = header.FileName;
                        break;
                    }

                    case "Content-Type":
                    {
                        var header = MediaTypeHeaderValue.Parse(headerValue);
                        contentType = header.MediaType;
                        break;
                    }
                    }
                }

                var           endBoundary = IndexOf(buffer, position, boundaryBytes);
                var           dataBuffer  = ReadBuffer(buffer, ref position, endBoundary - position);
                Func <string> getText     = () =>
                {
                    var s = Encoding.UTF8.GetString(dataBuffer);
//                    s = s.Substring(0, s.Length - 2);
                    return(s);
                };
                HttpBody body;
                switch (contentType)
                {
                case "text/plain":
                    body = new StringHttpBody(getText());
                    break;

                case "application/json":
                    body = new JsonHttpBody(JToken.Parse(getText()));
                    break;

                case "application/octet-stream":
                    body = new ByteArrayHttpBody {
                        Data = dataBuffer
                    };
                    break;

                default:
                    throw new Exception($"Unsupported media type: {contentType}");
                }
                result.Data[name] = new MultipartData {
                    FileName = fileName, Body = body
                };

                var endBoundaryLine = ReadLine(buffer, ref position);
                if (endBoundaryLine == boundaryLine + "--")
                {
                    break;
                }
                if (endBoundaryLine != boundaryLine)
                {
                    throw new Exception($"Expected ending boundary but found: {boundaryLine}");
                }
            }
            return(result);
        }
 public Task <byte[]> VisitByteArrayBodyAsync(ByteArrayHttpBody body)
 {
     return(Task.FromResult(body.Data));
 }
Beispiel #7
0
 public string VisitByteArrayBody(ByteArrayHttpBody body)
 {
     return("application/octet-stream");
 }