Upload and download files on the Http server (4) (1)
I. Introduction
You are welcome to compile an Http server with me to upload and download files. Now I will review what I mentioned earlier. Chapter 1 and Chapter 2 explain the overall HTTP trend, chapter 3 implements underlying network programming. Next, I want to talk about request retrieval and response content. Here we will mainly explain the response content. Why? Because we write an HTTP server that interacts with the browser, we only perform passive responses in most cases.
This is a type of question-answer question. In fact, when I explained this chapter, I was going to explain some Linux signal interruptions. The system sends some signals to our applications at the network layer, which may lead to unexpected termination of our applications. But when I wrote this blog, I gave up again. I want to raise another interruption capture when talking about the process. In fact, the real design of the Request Response layer class requires a lot of content. Here is the design of the HttpResponse class and HttpRequest class. in j2EE, we use these two classes when writing Servlet, such as HttpServletResquest, HttpServletResponse class, if you are interested in the content here, you can download tomcat, there are these classes in the servlet-api.jar package.
In the implementation of this article, the Request class contains only one method to get the Request header and the resolution header. I have explained how to parse headers in Http Server File upload and download (I). Readers only need to encapsulate a class for it.
Ii. HttpRequest class
The request message is parsed by the HttpRequest class defined in the namespace as Http. The constructor of this class accepts a socket, that is, the Socket connected to us. We have already talked about it at the network layer, and then call server_read () in the getHeader method to obtain the request header, then, it is parsed using the Utils: parseHeader () function. In this way, the parsed content is put into the required string, and the currently unnecessary content is directly in the map. Here I paste the Code directly, and it seems easier for everyone. Here I will mainly explain the download of files in this chapter, so I will mainly analyze the HttpResponse class, while the HttpRequest class only posts the content that is currently needed.
Header file (include/httprequest. h)
1 #ifndef HTTPREQUEST_H2 #define HTTPREQUEST_H3 #include "socket.h"4 #include 5 #include
6 #include
7 namespace Http{8 class HttpRequest{9 public:10 HttpRequest(TCP::Socket &c);11 virtual ~HttpRequest();12 std::map13 ......14 protected:15 private:16 std::string method;17 std::string url;18 std::string host;19 TCP::Socket &s;20 };21 }2223 #endif // HTTPREQUEST_H
Source file (src/httprequest. cpp)
1 #include "httprequest.h"2 #include "utils.h"3 namespace Http{4 HttpRequest::HttpRequest(TCP::Socket &c):s(c){5 }67 HttpRequest::~HttpRequest(){8 }9 std::map10 char recvBuf[1024];11 memset(recvBuf,0,sizeof(recvBuf));12 s.server_read(confd,recvBuf,1024);13 std::cout<14 std::map15 method =mp["Method"];16 url=mp["Url"];17 host=mp["Host"];18 return mp;19 }20 ......21 }
Iii. HttpResponse class
When we access the Http server, the browser displays the content of the files that can be downloaded. Then, we click the files to be downloaded and the files can be downloaded. First, when I click the URL of this file, the browser sends us some request headers. For example, it sends a file named/download/httpserver.zip, indicating the file he downloaded and the file named httpserver.zip. We can use getHeader to capture the request header and then obtain the URL. After that, the server will send a response header, telling the browser that your request is approved. The request header ends with an empty behavior tag, followed by the content of the specific file.
When sending a response header, you still need to send the Protocol version, status code, response content type, file length, file breakpoint download, and other content, or use chunk for transmission during transmission, but here I use the length of the file to mark it. You can view other transmission methods. Note that the size of the transmitted object must be specified in the Response Header. Otherwise, the client may refuse to receive the bytes sent by the server if it does not know when it will end. In this class, when sending a request to download a file, I use the sendFile function. This function reads the file in binary mode, in the response header, the browser is also notified to receive files in binary mode. In this way, files are read and sent in binary mode. The sendLineFile and sendIndexFile are roughly the same. They both use ASCII text to send content. For example, the HTML content that needs to be displayed in the browser can be used through these two functions. The function name indicates that sendLineFile will be read as a file line, while the sendIndexFile will write the content on the same line. For example, when a browser requests an index.html file, the display effect of two sendlinefiles and sendIndexFile is the same. However, if you right-click the source code to view the sendLineFile, the content of the sendLineFile is the same as that of the source file, the content sent by sendIndexFile will be in the first line without line breaks.
After talking about this, you are also quite clear. Below are some specific codes.
Header file (include/httpresponse. h)
1 #ifndef HTTPRESPONSE_H2 #define HTTPRESPONSE_H3 #include "socket.h"4 #include
5 #include
6 #include
7 #include
8 #include9 #include
10 #include "utils.h"11 namespace Http{12 class HttpResponse{13 public:14 HttpResponse(TCP::Socket &c);15 virtual ~HttpResponse();16 ssize_t send(int confd,std::string content);17 ssize_t sendIndexFile(int confd,std::string FileName);18 ssize_t sendFile(int &confd,std::string FileName,int64_t pos);19 ssize_t sendLineFile(int confd,std::string file);20 void setProtocal(std::string);21 void setStatusCode(std::string);22 void setServerName(std::string);23 void setContentType(std::string);24 void setContentRange(std::string);25 void setContentLength(int64_t);26 protected:27 std::string getHeader() const;28 private:29 std::string protocal;30 std::string statusCode;31 std::string serverName;32 std::string contentType;33 std::string contentLength;34 std::string contentRange;35 std::string connection;36 std::string date;37 TCP::Socket &s;38 };39 }40 #endif // HTTPRESPONSE_H