public UInt32 BuildHttpResponseFromFirstLine(ByteBuilder httpBuilder, String firstLineOfHttpRequest) { String[] httpStrings = firstLineOfHttpRequest.Split(new Char[] { ' ' }, 3); String resourceString = HttpUtility.UrlDecode(httpStrings[1]); httpBuilder.AppendAscii(httpStrings[2]); httpBuilder.AppendAscii(' '); if (resourceString.Equals("/favicon.ico")) { httpBuilder.AppendAscii("200 OK\r\nConnection: close\r\nAccess-Control-Allow-Origin: *\r\nContent-Type: text/html\r\nContent-Length: "); httpBuilder.AppendNumber(DefaultFavIcon.Length); httpBuilder.AppendAscii(Http.DoubleNewline); httpBuilder.AppendAscii(DefaultFavIcon); return(0); } else { return(BuildHttpResponseFromResource(httpBuilder, resourceString)); } }
// Returns the offset of the http response in the text builder public UInt32 BuildHttpResponseFromResource(ByteBuilder httpBuilder, String resourceString) { // Append the headers (leave space for content length) httpBuilder.AppendAscii("200 OK\r\nConnection: close\r\nAccess-Control-Allow-Origin: *\r\nContent-Type: text/html\r\nContent-Length: ??????????\r\n\r\n"); uint contentOffset = httpBuilder.contentLength; BuildHtmlResponse(httpBuilder, resourceString); uint contentLength = httpBuilder.contentLength - contentOffset; // Insert the content length String contentLengthString = contentLength.ToString(); if (contentLengthString.Length > 10) { throw new InvalidOperationException(String.Format("CodeBug: content length {0} is too big", contentLengthString)); } for (int i = 0; i < contentLengthString.Length; i++) { httpBuilder.bytes[contentOffset - 4 - contentLengthString.Length + i] = (byte)contentLengthString[i]; } // Shift the headers UInt32 shift = 10 - (uint)contentLengthString.Length; // 10 characters were reserved for the content length if (shift > 0) { for (int i = 0; i <= (contentOffset - 15); i++) { httpBuilder.bytes[contentOffset - 5 - contentLengthString.Length - i] = httpBuilder.bytes[contentOffset - 5 - contentLengthString.Length - i - shift]; } } return(shift); }