Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunny committed Mar 5, 2019
0 parents commit b4ad1d6
Show file tree
Hide file tree
Showing 18 changed files with 2,030 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
lib
*nuspec
*nupkg
nupkgs
*.swp
*.*~
bin/
obj/
.vs/
.vscode/
.DS_Store
.git/
Debug.log
45 changes: 45 additions & 0 deletions Content/ByteContent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.IO;

namespace Yove.Http
{
public class ByteContent : HttpContent
{
internal byte[] Content { get; set; }

internal int Offset { get; set; }
internal int Count { get; set; }

public override long ContentLength
{
get
{
return Content.LongLength;
}
}

public ByteContent() { }

public ByteContent(byte[] Content) : this(Content, 0, Content.Length) { }

public ByteContent(byte[] Content, int Offset, int Count)
{
if (Content == null || Offset < 0 || Count < 0 || Offset > Content.Length || Count > (Content.Length - Offset))
throw new ArgumentNullException("Parameters is empty or invalid value");

this.Content = Content;
this.Offset = Offset;
this.Count = Count;
}

public override void Write(Stream CommonStream)
{
if (CommonStream == null)
throw new ArgumentNullException("Stream is empty");

CommonStream.Write(Content, Offset, Count);
}

public override void Dispose() { }
}
}
20 changes: 20 additions & 0 deletions Content/FileContent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.IO;
using System;

namespace Yove.Http
{
public class FileContent : StreamContent
{
internal string Path { get; set; }

public FileContent(string Path, int BufferSize = 32768)
{
if (string.IsNullOrEmpty(Path))
throw new ArgumentNullException("Path is null or empty");

this.Content = new FileStream(Path, FileMode.Open, FileAccess.Read);
this.BufferSize = BufferSize;
this.Path = Path;
}
}
}
17 changes: 17 additions & 0 deletions Content/HttpContent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.IO;
using System.Text;

namespace Yove.Http
{
public abstract class HttpContent
{
public string ContentType { get; set; }

public abstract long ContentLength { get; }

public abstract void Write(Stream CommonStream);

public abstract void Dispose();
}
}
245 changes: 245 additions & 0 deletions Content/MultipartContent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Linq;

namespace Yove.Http
{
public class MultipartContent : HttpContent, IEnumerable<HttpContent>
{
private sealed class Element
{
public string Name { get; set; }
public string Filename { get; set; }

public HttpContent Content { get; set; }

public bool IsFieldFile
{
get
{
return Filename != null;
}
}
}

private string Boundary { get; set; }

private List<Element> Elements = new List<Element>();

public override long ContentLength
{
get
{
if (Elements.Count == 0)
throw new ObjectDisposedException("Content disposed or empty");

long Length = 0;

foreach (Element Item in Elements)
{
Length += Item.Content.ContentLength;

if (Item.IsFieldFile)
{
Length += 72;
Length += Item.Name.Length;
Length += Item.Filename.Length;
Length += Item.Content.ContentType.Length;
}
else
{
Length += 43;
Length += Item.Name.Length;
}

Length += Boundary.Length + 6;
}

return Length += Boundary.Length + 6;
}
}

public MultipartContent() : this($"----------------{HttpUtils.RandomString(16)}") { }

public MultipartContent(string Boundary)
{
if (string.IsNullOrEmpty(Boundary))
throw new ArgumentNullException("Boundary is null or empty");

this.Boundary = Boundary;
this.ContentType = $"multipart/form-data; boundary={Boundary}";
}

public void Add(string Name, HttpContent Content)
{
if (string.IsNullOrEmpty(Name))
throw new ArgumentNullException("Name is null or empty");

if (Content == null)
throw new ArgumentNullException("Content is null");

Elements.Add(new Element
{
Name = Name,
Content = Content
});
}

public void Add(string Name, HttpContent Content, string Filename)
{
if (string.IsNullOrEmpty(Name))
throw new ArgumentNullException("Name is null or empty");

if (string.IsNullOrEmpty(Filename))
throw new ArgumentNullException("Filename is null or empty");

if (Content == null)
throw new ArgumentNullException("Content is null");

Content.ContentType = "multipart/form-data";

Elements.Add(new Element
{
Name = Name,
Filename = Filename,
Content = Content
});
}

public void Add(string Name, string ContentType, HttpContent Content, string Filename)
{
if (string.IsNullOrEmpty(Name))
throw new ArgumentNullException("Name is null or empty");

if (string.IsNullOrEmpty(Filename))
throw new ArgumentNullException("Filename is null or empty");

if (string.IsNullOrEmpty(ContentType))
throw new ArgumentNullException("ContentType is null or empty");

if (Content == null)
throw new ArgumentNullException("Content is null");

Content.ContentType = ContentType;

Elements.Add(new Element
{
Name = Name,
Filename = Filename,
Content = Content
});
}

public void Add(string Name, FileContent Content, string Filename = null)
{
if (string.IsNullOrEmpty(Name))
throw new ArgumentNullException("Name is null or empty");

if (Filename == null)
{
if (Content.Path.Split('/').Last().Contains("."))
Filename = Content.Path.Split('/').Last();
else
throw new ArgumentNullException("Path is null or empty");
}

if (Content == null)
throw new ArgumentNullException("Content is null");

Content.ContentType = "multipart/form-data";

Elements.Add(new Element
{
Name = Name,
Filename = Filename,
Content = Content
});
}

public void Add(string Name, string ContentType, FileContent Content, string Filename = null)
{
if (string.IsNullOrEmpty(Name))
throw new ArgumentNullException("Name is null or empty");

if (Filename == null)
{
if (Content.Path.Split('/').Last().Contains("."))
Filename = Content.Path.Split('/').Last();
else
throw new ArgumentNullException("Path is null or empty");
}

if (string.IsNullOrEmpty(ContentType))
throw new ArgumentNullException("ContentType is null or empty");

if (Content == null)
throw new ArgumentNullException("Content is null");

Content.ContentType = ContentType;

Elements.Add(new Element
{
Name = Name,
Filename = Filename,
Content = Content
});
}

public override void Write(Stream CommonStream)
{
if (Elements.Count == 0)
throw new ObjectDisposedException("Content disposed or empty");

if (CommonStream == null)
throw new ArgumentNullException("CommonStream is null");

byte[] LineBytes = Encoding.ASCII.GetBytes("\r\n");
byte[] BoundaryBytes = Encoding.ASCII.GetBytes($"--{Boundary}\r\n");

foreach (Element Item in Elements)
{
CommonStream.Write(BoundaryBytes, 0, BoundaryBytes.Length);

string Field = string.Empty;

if (Item.IsFieldFile)
Field = $"Content-Disposition: form-data; name=\"{Item.Name}\"; filename=\"{Item.Filename}\"\r\nContent-Type: {Item.Content.ContentType}\r\n\r\n";
else
Field = $"Content-Disposition: form-data; name=\"{Item.Name}\"\r\n\r\n";

byte[] FieldBytes = Encoding.ASCII.GetBytes(Field);

CommonStream.Write(FieldBytes, 0, FieldBytes.Length);
Item.Content.Write(CommonStream);
CommonStream.Write(LineBytes, 0, LineBytes.Length);
}

BoundaryBytes = Encoding.ASCII.GetBytes($"--{Boundary}--\r\n");
CommonStream.Write(BoundaryBytes, 0, BoundaryBytes.Length);
}

public override void Dispose()
{
if (Elements.Count > 0)
{
foreach (Element Item in Elements)
Item.Content.Dispose();

Elements.Clear();
}
}

public IEnumerator<HttpContent> GetEnumerator()
{
throw new NotImplementedException();
}

IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}
}
Loading

0 comments on commit b4ad1d6

Please sign in to comment.