First, Introduction
Welcome to join me to write the HTTP server implementation file upload and download, now I review some of the above mentioned in the previous section, I have mentioned the download of the file, in the file download also mentions that the continuation of the file download only need to fill in the response header content-range this field, And the server's file pointer points to the read at the specified location to begin the read transfer. In this chapter I explain the function of uploading files, finish this chapter, the general function is complete, followed by the above file control module and some resource modules.
In the file upload mainly to HttpRequest class, in consideration of the file upload I am a bit confused, in the end the upload function of the file is placed in the HttpResponse or under the HttpRequest, after all, httpresponse some of the corresponding file download function, The ability to add a file upload is not too. But I finally chose in the HttpRequest, the reason is that I mainly httpresponse as a server to the browser to send content, and HttpRequest as a browser to the server to send content. The functions of downloading and uploading are located on the HttpResponse and HttpRequest respectively.
After completing the attribution of the function, and then directly on the code, in the file upload, the C + + stream is involved. It's not a lot of content here, but it's an important chunk of C + +. Have time to review this piece of content with everyone. OK, then on the code, the previous chapter of the content has designed some HttpRequest code, not all included in.
Second, HttpRequest
Header file (include/httprequest.h)
1 #ifndef httprequest_h 2 #define HTTPREQUEST_H 3 #include "socket.h" 4 #include <map> 5 #include <string> 6 # Include <fstream> 7 namespace http{8 class httprequest{9 public:10 HttpRequest (tcp::socket &c) ; one virtual ~httprequest (); std::string GetMethod () const;13 std::string getUrl () const;14 std::string gethost () const;15 std::map<std::string,std::string> getheader (int confd); ssize_t upload (int confd,std::string filename); protected:18 private:19 std::string method;20 std::string url;21 std::string host;22 tcp::socket &s;23 };24}25 #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} 6 7 httprequest::~httprequest () {8} 9 std::map<std::string,std::string> Httprequest::gethead ER (int confd) {Ten char recvbuf[1024];11 memset (recvbuf,0,sizeof (RECVBUF)); S.server_read (confd,re cvbuf,1024) std::cout<<recvbuf<<std::endl;14 std::map<std::string,std::string> MP =Ut Ils::p arseheader (Recvbuf), Method =mp["method"];16 url=mp["url"];17 host=mp["host"];18 R Eturn mp;19}20 ssize_t httprequest::upload (int confd,std::string filename) {+ Char buf[1024];22 s ize_t n=0;23 ssize_t nread=0;24 std::string boundary;25 std::string file;26 std::ofstream outstream;27 int readlinecount=1;28 while (1) {buf memset (buf,0,sizeof (n=s)); Server_readline (Confd,buF,sizeof (BUF)), if (readlinecount==1) {boundary=std::string (Buf,buf+strlen (BUF)-2); 33 boundary+= "--\r\n"; Std::cout<<boundary<<std::endl<<boundary.size (); 35 }else if (readlinecount==2) {$ 38 int i=n;37 while (buf[i]!= ' = ') if ((buf[i]>= ' 0 ' &&buf[i]<= ' 9 ') 39 | | (buf[i]>= ' A ' &&buf[i]<= ' z ') 40 | | (buf[i]>= ' A ' &&buf[i]<= ' Z ') 41 | | (buf[i]== '. ')) i--;43 else{44 buf[i]= ' * '; -;46}47}48 file=std::string (buf+i+2,buf+n-3);}else if (readlinecount==3) {std::string rw;51 rw=std::string (Buf,buf+strlen (BUF)); int pos=rw.fi nd ('/');W=rw.substr (0,pos); filename=filename+file;55 if (rw== "Content-type:text") 56 Outstream.open (Filename.c_str ()); else{58 Outstream.open (Filename.c_str (), S td::ios::binary) std::cout<< "Ios::binary" <<std::endl;60}61 }else if (readlinecount==4) {memset (buf,0,sizeof (BUF)), and (1) {64 N=s.server_readn (confd,buf,sizeof (BUF)); if (N==boundary.size () &&strcmp (Buf,boundary.c_str ( ) ==0) {exit;67 goto}68 nread+=n;69 if (buf[n-1]==0) {outstream.write (buf,n-1);}else{72 Outstream.write (buf,n);}74}75}76 readlinecount++;77 }78 exit:79 Outstream.close (); S.server_close (CONFD); Bayi return nread;82}83 std::string HttpRequest: : GetMethod () const{84 return method;85}86 std::string httprequest::geturl () const{87 return URL; }89 std::string httprequest::gethost () const{90 return host;91}92}
All right, the code for uploading the file has come out, and now it's just a little bit of an explanation. Before explaining the code, let's look at what the browser sends to the server when we click the Upload file button, for example, I have a test.txt text file (where the file file is used for good viewing, but the binaries are also consistent). The Test.txt file contains only one line of the AAABBB 6 letters. Then open the Firefox Developer Network feature. And when you click Send file, you can see the following information on the message header.
These are already mentioned in the previous chapters and are not repeated here, and the option to click on the parameters can be seen in the following information.
Here 1th, line 2 is the content of the request header, followed by a blank line followed by the request body 4-9 rows. See the content of the request body is not directly test.txt content. Show '--23469111452 ' as the beginning, apprentice this is the delimiter of the text. The previous fixed section '-', plus a browser automatically generated data. And the interpretation of a document is the same, but the number of 2 more '-'. In the 5th, line 6 is the description of the uploaded file. followed by a blank line. The beginning of line 8th is the contents of the file. Once you know the request body, it's easy to write the code. The Readlinecount variable in the upload above is the positioning function. Look at the server has received the line, here S.server_readn this line number is not submitted, now the code snippet has been modified, so some with the blog a little difference, in general still consistent.
Written here, basically completed httpserver this file upload and download function. Then it's a combination of these pieces. The next chapter, "HTTP Server implementation file upload and download (vi)" is explained.
HTTP server implementation file upload and download (v)