Esempio n. 1
0
        // write response body to file
        public static void writeResponseIntoFile(string headers, string body)
        {
            string contentType = MessageParser.getHeaderValue(headers, HttpHeaders.ContentType);

            try
            {
                if (contentType != string.Empty)
                {
                    switch (contentType)
                    {
                    case "text/plain":
                        File.WriteAllText("..\\..\\resources\\text\\temp-file.txt", body);
                        Console.WriteLine("writed into temp-file.txt");
                        break;

                    case "text/html":
                        File.WriteAllText("..\\..\\resources\\web\\temp-page.html", body);
                        Console.WriteLine("writed into temp-page.html");
                        break;

                    case "image/jpeg":
                        File.WriteAllText("..\\..\\resources\\images\\temp-image.jpg", body);
                        Console.WriteLine("writed into temp-image.jpg");
                        break;

                    case "image/png":
                        File.WriteAllText("..\\..\\resources\\images\\temp-image.png", body);
                        Console.WriteLine("writed into temp-image.png");
                        break;

                    default:
                        File.WriteAllText("..\\..\\resources\\web\\temp-page.html", body);
                        Console.WriteLine("writed into default file");
                        break;
                    }
                }
                else
                {
                    Console.WriteLine("Error");
                }
            } catch (IOException)
            {
                Console.WriteLine("IO exception");
            }
        }
Esempio n. 2
0
        private static void readBody(DataObject response, Socket socket)
        {
            // header is received, parsing content length
            string contentLengthMatch = MessageParser.getHeaderValue(response.getStringRepresentation(),
                                                                     HttpHeaders.ContentLength);

            string transferEncodingMatch = MessageParser.getHeaderValue(response.getStringRepresentation(),
                                                                        HttpHeaders.TransferEncoding);

            if (contentLengthMatch != string.Empty)
            {
                Console.WriteLine(contentLengthMatch);
                int    contentLength = int.Parse(contentLengthMatch);
                byte[] bodyBuff;
                int    receivedLength = 0;
                // read the body
                for (int i = 0; i < contentLength; i++)
                {
                    bodyBuff       = new byte[1];
                    receivedLength = socket.Receive(bodyBuff, 0, 1, 0);
                    response.appendStringRepresentation(bodyBuff);
                }
            }
            else if (transferEncodingMatch == "chunked")
            {
                byte[]     bodyBuff;
                byte[]     lengthBuff;
                DataObject currentLength = new DataObject();
                // while there are available bytes of data to receive
                while (socket.Available > 0)
                {
                    // receiving length of chunk
                    lengthBuff = new byte[1];
                    socket.Receive(lengthBuff, 0, 1, 0);
                    currentLength.appendStringRepresentation(lengthBuff);
                    // if length of chunk is known
                    if (currentLength.getStringRepresentation().Contains("\r\n"))
                    {
                        // format length of chunk from hexadecimal to decimal
                        string hexNumberFormat = currentLength.getStringRepresentation().Substring(
                            0, currentLength.getStringRepresentation().IndexOf("\r\n"));
                        int length = Convert.ToInt32(hexNumberFormat, 16);
                        // end of body
                        if (length == 0)
                        {
                            break;
                        }
                        int receivedLength = 0;
                        // receiving chunk
                        for (int i = 0; i < length + 2; i++)
                        {
                            bodyBuff        = new byte[1];
                            receivedLength += socket.Receive(bodyBuff, 0, 1, 0);
                            response.appendStringRepresentation(bodyBuff);
                        }
                        // clear length of next chunk
                        currentLength.clear();
                    }
                }
            }
        }