Repository navigation
Expand file tree
/
Copy pathResponse.cs
More file actions
63 lines (53 loc) · 1.83 KB
/
Copy pathResponse.cs
File metadata and controls
63 lines (53 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace HTTPServer
{
public enum StatusCode
{
OK = 200,
InternalServerError = 500,
NotFound = 404,
BadRequest = 400,
Redirect = 301
}
class Response
{
string responseString;
StatusCode code;
List<string> headerLines = new List<string>();
public string ResponseString
{
get
{
return responseString;
}
}
public Response(StatusCode code, string contentType, string content, string redirectoinPath)
{
// TODO: Add headlines (Content-Type, Content-Length,Date, [location if there is redirection])
headerLines.Add("Content-Type: " + contentType + "\r\n");
headerLines.Add("Content-length: " + content.Length.ToString() + "\r\n");
headerLines.Add("Date: " + DateTime.Now.ToString() + "\r\n");
if (!string.IsNullOrEmpty(redirectoinPath))
{
headerLines.Add("Location: " + redirectoinPath + "\r\n");
}
string statusLine = GetStatusLine(code);
// TODO: Create the response string
responseString = statusLine;
foreach (string line in headerLines)
responseString += line;
responseString += "\r\n" + content;
}
private string GetStatusLine(StatusCode code)
{
// TODO: Create the response status line and return it
int statuscode = (int)code;
string statusLine = Configuration.ServerHTTPVersion + " " + statuscode.ToString() + " " + code.ToString();
return statusLine;
}
}
}