Mercurial > hg > ucis.core
comparison Net/HTTP.cs @ 7:4b78cc5f116b
Fixes and improvements (some untested)
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Sun, 13 Jan 2013 18:44:17 +0100 |
parents | 5ce7a138fdba |
children | 7269e91c6e26 |
comparison
equal
deleted
inserted
replaced
6:5ce7a138fdba | 7:4b78cc5f116b |
---|---|
169 } | 169 } |
170 } | 170 } |
171 return; | 171 return; |
172 | 172 |
173 SendError400AndClose: | 173 SendError400AndClose: |
174 State = HTTPConnectionState.ProcessingRequest; | |
174 SendErrorAndClose(400); | 175 SendErrorAndClose(400); |
175 return; | 176 return; |
176 SendError500AndClose: | 177 SendError500AndClose: |
178 State = HTTPConnectionState.ProcessingRequest; | |
177 SendErrorAndClose(500); | 179 SendErrorAndClose(500); |
178 return; | 180 return; |
179 } | 181 } |
180 | 182 |
181 public String GetRequestHeader(String name) { | 183 public String GetRequestHeader(String name) { |
284 if (c.Value != null) { | 286 if (c.Value != null) { |
285 c.Value.ServeRequest(context); | 287 c.Value.ServeRequest(context); |
286 } else { | 288 } else { |
287 context.SendErrorAndClose(404); | 289 context.SendErrorAndClose(404); |
288 } | 290 } |
291 } | |
292 } | |
293 public class HTTPStaticContent : IHTTPContentProvider { | |
294 public ArraySegment<Byte> ContentBuffer { get; set; } | |
295 public String ContentType { get; set; } | |
296 public HTTPStaticContent() : this(new ArraySegment<Byte>()) { } | |
297 public HTTPStaticContent(ArraySegment<Byte> content) : this(content, "application/octet-stream") { } | |
298 public HTTPStaticContent(String content, String contentType) : this(Encoding.UTF8.GetBytes(content), contentType) { } | |
299 public HTTPStaticContent(String contentType) : this(new ArraySegment<Byte>(), contentType) { } | |
300 public HTTPStaticContent(Byte[] content, String contentType) : this(new ArraySegment<Byte>(content), contentType) { } | |
301 public HTTPStaticContent(ArraySegment<Byte> content, String contentType) { | |
302 this.ContentBuffer = content; | |
303 this.ContentType = contentType; | |
304 } | |
305 public void SetContent(Byte[] bytes) { ContentBuffer = new ArraySegment<byte>(bytes); } | |
306 public void SetContent(Byte[] bytes, int offset, int count) { ContentBuffer = new ArraySegment<byte>(bytes, offset, count); } | |
307 public void SetContent(String content, Encoding encoding) { SetContent(encoding.GetBytes(content)); } | |
308 public void SetContent(String content) { SetContent(content, Encoding.UTF8); } | |
309 public void ServeRequest(HTTPContext context) { | |
310 ArraySegment<Byte> content = ContentBuffer; | |
311 if (content.Array == null) { | |
312 context.SendErrorAndClose(404); | |
313 return; | |
314 } | |
315 String contentType = ContentType; | |
316 context.SendStatus(200); | |
317 if (contentType != null) context.SendHeader("Content-Type", contentType); | |
318 context.SendHeader("Content-Length", content.Count.ToString()); | |
319 Stream response = context.GetResponseStream(); | |
320 response.Write(content.Array, content.Offset, content.Count); | |
321 response.Close(); | |
289 } | 322 } |
290 } | 323 } |
291 public class HTTPFileProvider : IHTTPContentProvider { | 324 public class HTTPFileProvider : IHTTPContentProvider { |
292 public String FileName { get; private set; } | 325 public String FileName { get; private set; } |
293 public String ContentType { get; private set; } | 326 public String ContentType { get; private set; } |
345 int fsizei = Convert.ToInt32(fsize, 8); | 378 int fsizei = Convert.ToInt32(fsize, 8); |
346 if (reqname1.Equals(fname, StringComparison.OrdinalIgnoreCase) || reqname2.Equals(fname)) { | 379 if (reqname1.Equals(fname, StringComparison.OrdinalIgnoreCase) || reqname2.Equals(fname)) { |
347 context.SendStatus(200); | 380 context.SendStatus(200); |
348 context.SendHeader("Content-Length", fsizei.ToString()); | 381 context.SendHeader("Content-Length", fsizei.ToString()); |
349 String ctype = null; | 382 String ctype = null; |
350 switch (Path.GetExtension(fname).ToUpperInvariant()) { | 383 switch (Path.GetExtension(fname).ToLowerInvariant()) { |
351 case ".txt": ctype = "text/plain"; break; | 384 case ".txt": ctype = "text/plain"; break; |
352 case ".htm": | 385 case ".htm": |
353 case ".html": ctype = "text/html"; break; | 386 case ".html": ctype = "text/html"; break; |
354 case ".css": ctype = "text/css"; break; | 387 case ".css": ctype = "text/css"; break; |
355 case ".js": ctype = "application/x-javascript"; break; | 388 case ".js": ctype = "application/x-javascript"; break; |