public string BuildRelativeUrl()
        {
            var url = (Path[0] == '/' ? "" : "/") + Path;

            if (QueryString != null && QueryString.Any())
            {
                url += "?" + FormUrlEncoder.Encode(QueryString);
            }

            return(url);
        }
Exemple #2
0
        public void Write(HttpRequest request)
        {
            long   len       = 0;
            Action writeBody = null;

            if (request.Uploads != null && request.Uploads.Length > 0)
            {
                request.SetMultipart();
            }

            if (!request.IsMultipart)
            {
                if (request.PostValues != null)
                {
                    request.ResetField(HttpField.ContentType, MimeType.FormUrlEncoded);
                    var fvps = FormUrlEncoder.Encode(request.PostValues);
                    var body = Encoding.UTF8.GetBytes(fvps);
                    len       = body.Length;
                    writeBody = () => WriteToStream(body, 0, body.Length);
                }
                else
                {
                    len       = 0;
                    writeBody = () => { };
                }
            }
            else
            {
                IEnumerable <FormData> formData = null;

                if (request.PostValues != null)
                {
                    formData = request.PostValues
                               .Select(x =>
                    {
                        var d = new FormData(x.Value);
                        d.SetField(
                            HttpField.ContentDisposition,
                            ContentDisposition.FormData + "; name=\"" + x.Field + "\"");
                        d.SetHeader();
                        return(d);
                    });
                }

                if (request.Uploads != null)
                {
                    var fileFormData = request.Uploads
                                       .Select(x =>
                    {
                        var d = new FormData(x);
                        d.SetField(
                            HttpField.ContentDisposition,
                            ContentDisposition.FormData + "; name=\"" + x.Name + "\"; filename=\"" + x.Filename + "\"");
                        d.SetField(HttpField.ContentType, x.FileMimeType);
                        d.SetHeader();
                        return(d);
                    });

                    formData = formData != null?formData.Concat(fileFormData) : fileFormData;
                }

                if (formData != null)
                {
                    len =
                        (formData.Count() * (request.Boundary.Length + 4)) +
                        (request.Boundary.Length + 6) +
                        formData.Sum(x => x.Header.Length) +
                        (formData.Sum(x => x.StringValue != null ?
                                      x.StringValue.Length :
                                      x.UploadValue.Stream.Length) + 2);
                    writeBody = () =>
                    {
                        var startBoundary = Encoding.UTF8.GetBytes("--" + request.Boundary + "\r\n");
                        var endOfValue    = new[] { (byte)'\r', (byte)'\n' };
                        var endBoundary   = Encoding.UTF8.GetBytes("--" + request.Boundary + "--\r\n");
                        foreach (var part in formData)
                        {
                            WriteToStream(startBoundary, 0, startBoundary.Length);
                            var headerBytes = Encoding.UTF8.GetBytes(part.Header);
                            WriteToStream(headerBytes, 0, headerBytes.Length);

                            if (part.StringValue != null)
                            {
                                var buffer = Encoding.UTF8.GetBytes(part.StringValue);
                                WriteToStream(buffer, 0, buffer.Length);
                            }
                            else
                            {
                                part.UploadValue.Stream.Position = 0;
                                part.UploadValue.Stream.CopyTo(Stream);
                            }
                            WriteToStream(endOfValue, 0, endOfValue.Length);
                        }

                        WriteToStream(endBoundary, 0, endBoundary.Length);
                    };
                }
                else
                {
                    writeBody = () => { };
                    len       = 0;
                }
            }

            if (request.Method == HttpMethod.Post)
            {
                request.ResetField(HttpField.ContentLength, len);
            }

            if (!request.HasField(HttpField.Host))
            {
                request.ResetField(HttpField.Host, Host);
            }

            var headerStr = request.CreateHeader(Host);
            var header    = Encoding.UTF8.GetBytes(headerStr);

            WriteToStream(header, 0, header.Length);
            writeBody();
            Stream.Flush();
        }