-
-
Notifications
You must be signed in to change notification settings - Fork 23
Fixing basic MIME types #214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -371,7 +371,7 @@ public bool Start() | |||||
/// <summary> | ||||||
/// Restart the server. | ||||||
/// </summary> | ||||||
private bool Restart() | ||||||
public bool Restart() | ||||||
{ | ||||||
Stop(); | ||||||
return Start(); | ||||||
|
@@ -468,7 +468,8 @@ public static void SendFileOverHTTP(HttpListenerResponse response, StorageFile s | |||||
/// /// <param name="contentType">The type of file, if empty string, then will use auto detection</param> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "double comment" here
Suggested change
|
||||||
public static void SendFileOverHTTP(HttpListenerResponse response, string fileName, byte[] content, string contentType = "") | ||||||
{ | ||||||
contentType = contentType == "" ? GetContentTypeFromFileName(fileName.Substring(fileName.LastIndexOf('.'))) : contentType; | ||||||
// If no extension, we will get the full file name | ||||||
contentType = contentType == "" ? GetContentTypeFromFileName(fileName.Substring(fileName.LastIndexOf('.') + 1)) : contentType; | ||||||
response.ContentType = contentType; | ||||||
response.ContentLength64 = content.Length; | ||||||
|
||||||
|
@@ -483,8 +484,6 @@ public static void SendFileOverHTTP(HttpListenerResponse response, string fileNa | |||||
// Writes data to output stream | ||||||
response.OutputStream.Write(content, (int)bytesSent, (int)bytesToSend); | ||||||
|
||||||
// allow some time to physically send the bits. Can be reduce to 10 or even less if not too much other code running in parallel | ||||||
|
||||||
// update bytes sent | ||||||
bytesSent += bytesToSend; | ||||||
} | ||||||
|
@@ -676,49 +675,58 @@ private void ListInterfaces() | |||||
/// <summary> | ||||||
/// Get the MIME-type for a file name. | ||||||
/// </summary> | ||||||
/// <param name="fileName">File name to get content type for.</param> | ||||||
/// <param name="fileExt">File extension to get content type for.</param> | ||||||
/// <returns>The MIME-type for the file name.</returns> | ||||||
private static string GetContentTypeFromFileName(string fileName) | ||||||
private static string GetContentTypeFromFileName(string fileExt) | ||||||
{ | ||||||
// normalize to lower case to speed comparison | ||||||
fileName = fileName.ToLower(); | ||||||
fileExt = fileExt.ToLower(); | ||||||
|
||||||
string contentType = "text/html"; | ||||||
|
||||||
//determine the type of file for the http header | ||||||
if (fileName == ".cs" || | ||||||
fileName == ".txt" || | ||||||
fileName == ".csproj" | ||||||
) | ||||||
if (fileExt == "cs" || | ||||||
fileExt == "txt" || | ||||||
fileExt == "csproj") | ||||||
{ | ||||||
contentType = "text/plain"; | ||||||
} | ||||||
else if (fileName == ".jpg" || | ||||||
fileName == ".bmp" || | ||||||
fileName == ".jpeg" || | ||||||
fileName == ".png" | ||||||
) | ||||||
else if (fileExt == "jpg" || | ||||||
fileExt == "jpeg" || | ||||||
fileExt == "jpe") | ||||||
{ | ||||||
contentType = "image/jpeg"; | ||||||
} | ||||||
else if (fileExt == "bmp" || | ||||||
fileExt == "png" || | ||||||
fileExt == "gif" || | ||||||
fileExt == "ief") | ||||||
{ | ||||||
contentType = "image"; | ||||||
contentType = $"image/{fileExt}"; | ||||||
} | ||||||
else if (fileName == ".htm" || | ||||||
fileName == ".html" | ||||||
) | ||||||
else if (fileExt == "htm" || | ||||||
fileExt == "html") | ||||||
{ | ||||||
contentType = "text/html"; | ||||||
} | ||||||
else if (fileName == ".mp3") | ||||||
else if (fileExt == "mp3") | ||||||
{ | ||||||
contentType = "audio/mpeg"; | ||||||
} | ||||||
else if (fileName == ".css") | ||||||
else if (fileExt == "css") | ||||||
{ | ||||||
contentType = "text/css"; | ||||||
} | ||||||
else if (fileName == ".ico") | ||||||
else if (fileExt == "ico") | ||||||
{ | ||||||
contentType = "image/x-icon"; | ||||||
} | ||||||
else if (fileExt == "zip" || | ||||||
fileExt == "json" || | ||||||
fileExt == "pdf") | ||||||
{ | ||||||
contentType = $"application/{fileExt}"; | ||||||
} | ||||||
|
||||||
return contentType; | ||||||
} | ||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one and the all the others in the comments are unrelated with the PR: fixing the MIME types. Can you please remove them and submit a separate PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, made this one private again, for the rest of the comments, that was breaking my build somehow (warnings are errors)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. So let's have those fixed first and merge the PR into this one.