| |||||||||||||||||||||||||||||||
|
![]() Generation HTTP of searches 1. Report HTTP. Introduction At once I want to specify one small thing. The terrible word the report is not that have solved other as the agreement of set of people, is simple once people: " Let's do{make} so and then all will be in the order ". To be afraid there is nothing, all is simple up to a disgrace and we shall open this disgrace now. So, what this such report HTTP and with what it eat? 1.1 The client and the server Miracles in the world and furthermore in the world programmizma and the Internet does not happen! Acquire it as firm true. And, if the program does not work or works not as it would be desirable, that, means, most likely, she either is written not correctly, or contains mistakes. So, how all the same the browser asks the server to send him though something? Yes it is very simple! It is necessary only a few{a little;little bit} to relax and start to take pleasure in process:-) 1.2. We write our the first HTTP search If you think, that all is too difficult, you are mistaken. The person so is arranged, that simply is not capable to create something complex , differently he in it will get confused:-) So, there is a browser and there is a Web-server. As the initiator of data exchange the browser always acts. The Web-server to anybody, it is never simple so will send nothing, that he has sent something to a browser - it is necessary, that the browser about it has asked. HTTP the search washes the elementary to look, for example, so: GET http://www.php.net/ HTTP/1.0rnrn * GET (In translation from English means "to receive") - type of search, the type of search can be miscellaneous, for example POST, HEAD, PUT, DELETE (from them we shall consider a part below). * http://www.php.net/ - URI (address) from which we want to receive though any information (naturally we hope to learn{teach} HTML page). * HTTP/1.0 - type and the version of the report which we shall use during dialogue with the server. * rn - the end of a line which it is necessary to repeat two times, what for, begins understandably a little later. You can execute the given search very simply. Start the program telnet.exe, enter as a host www.php.net, specify port 80, and simply type{collect} the given search, having pressed two times Enter in quality rnrn. In the answer you receive HTML a code of the main page of a site www.php.net. 1.3 Structure of search Let's consider, of what will consist HTTP search. All is simple enough. We shall begin that HTTP the search is quite intelligent text. Of what he will consist generally? We shall consider{examine} report HTTP 1.0. So: Request-Line [General-Header | Request-Header | Entity-Header] rn [Entity-Body] * Request-Line - a line of search * Format: " Method Request-URI HTTP-Versionrn " * Method - the method by which resource Request-URI will be processed, can be GET, POST, PUT, DELETE or HEAD. * Request-URI - the relative or absolute link to page with a set of parameters, for example,/index.html or http://www.myhost.ru/index.html or/index.html? a=1*b=qq. In the latter case the search with a set of variables a and b with corresponding values will be transferred{handed} to the server, and the sign "¿" - ampersant serves as a separator between parameters. * HTTP-Version - version HTTP of the report, in our case "HTTP/1.0". Methods of processing GET and POST are extremely interesting to us. Method GET it is possible to pass simply parameters in a script, and method POST it is possible to emulate submit forms. For method GET, Request-URI can look, for example, so: "/index.html? param1=1*param2=2 ". * General-Header - a body of heading. Format: [Date: valuen | Pragma: no-cachen] Can have only two parameters: Date or Pragma. Date - date across Greenwich in a format " Day of week, Number Month Year CHCH:MM:SS GMT ", for example, " Tue, 15 Nov 1994 08:12:31 GMT " - date of creation of search. Pragma can have one value no-cache which forbids caching page. * Request-Header - the part of heading describing search. Request-Header Can have the following parameters: Allow, Authorization, From, If-Modified-Since, Referer, User-Agent. In the given chapter{head} we shall not consider{examine} parameter Autorization as he is used for access to the closed resources that is required not too often. You can independently study formation of heading for the authorized access on a site www.w3c.org. * Allow - sets allowable methods of processing. Format: " Allow: GET | HEADn ". The parameter is ignored at the instruction{indication} of a method of processing POST in Request-Line. Sets allowable methods of processing of search. The proxy of the server do not modify parameter Allow and he in a constant kind reaches the server. * From - e-mail the address, sent search. Format: " From: adderssrn ". For example, " From: myname@mailserver.rurn ". * If-Modified-Since - specifies, that the search was not modified since such time. Format: " If-Modified-Since: datern " It is used only for a method of processing GET. Date is underlined across Greenwich in the same format, as well as for parameter Date in General-Header. * Referrer - the absolute link to page from which has taken place iniciacija search, i.e. the link to page from which the user has passed on ours. Format: " Referrer: urln ". Example: " Referrer: www.host.ru/index.htmln ". * User-Agent - type of a browser. For example: " User-Agent: Mozilla/4.0n " * Entity-Header - the part of heading describing data Entity-Body. In the given part of search parameters which describe a body of page are set. Entity-Header can contain the following parameters: Allow, Content-Encoding, Content-Length, Content-Type, Expires, Last-Modified, extension-header. * Allow - parameter similar Allow from General-Header. * Content-Encoding - type of coding of data Entity-Body. Format: " Sontent-Encoding: x-gzip | x-compress | another tipn ". Example: " Sontent-Encoding: x-gzipn ". The symbol "|" means a word "or", that is that either that or that i.t.d. Other type can specify a way of coding of the data, for example, for method POST: " Sontent-Encoding: application/x-www-form-urlencodedn ". * Content-Length - quantity{amount} of the bytes sent in Entity-Body. Value Content-Length has absolutely other sense for the data sent in format MIME where he acts as parameter of the description of a part of the data - "external/entity-body". Integers from zero and are allowable more. Example: " Content-Length: 26457n ". * Content-Type - type of the transmitted data. For example: " Content-Type: text/htmln ". * Expires - Time when the page should be removed from a cache of a browser. Format: " Expires: daten ". The format of date is alogical to a format of date for parameter Date from General-Header. * Last-Modified - time of last change of the sent data. Format: " Last-Modified: daten ". The format of date is alogical to a format of date for parameter Date from General-Header. * Extention-header - a part of heading which can intend, for example, for processing by a browser, or other program which accepts the document. In the given part it is possible to describe the parameters in a format " ParameterName: parametervaluen ". The given parameters will be ignored, if the program - client does not know, how them to process. For example: " Cookie: r=1rn " - establishes all known pechen`ki for page. And now after such terrible words let's try calm down and understand a little, what it is necessary to us? To understand we naturally we shall on examples. Let's present, that we should receive page from a site, having passed Cookies (Pechen`ki), differently us will simply send as nezvannykh visitors, and moreover, it is known, that on the given page let only after you have visited on the main page of a site. 2 Method GET Let's write our search. GET http://www.site.ru/news.html HTTP/1.0rn Host: www.site.rurn Referer: http://www.site.ru/index.htmlrn Cookie: income=1rn rn The given search speaks us that we want to receive contents of page to the address http://www.site.ru/news.html, I use method GET. Field Host speaks that the given page is on the server www.site.ru, field Referer speaks that behind news we came from the main page of a site, and field Cookie speaks that to us has been appropriated{given} such kuka. Why so fields Host, Referer and Sookie are important? Because normal programmers at creation of dynamic sites check the given fields, which appear in scripts (RNR including) as variables. For what it is necessary? For that, for example, that a site did not plunder, i.e. did not set on him the program for automatic uploading or that zashedshij on a site the person always would get on him only from the main page i.t.d. Now let's present, that we should fill in fields of the form on page and to send search from the form, let in the given form will be two fields: login and password (a login and the password), - and, we naturally know a login and the password. GET http: // www.site.ru/news.html? login=Petya%20Vasechkin*password=qq HTTP/1.0rn Host: www.site.rurn Referer: http://www.site.ru/index.htmlrn Cookie: income=1rn rn At us " Petya Vasechkin " Why we should write a login Petya%20Vasechkin? It iz=za that special symbols can be recognized by the server as attributes of presence of new parameter or the end of search i.t.d. Therefore there is an algorithm of coding of names of parameters and their values, in avoidance oshtbochnykh situations in search. The full description of the given algorithm can be found here, and in PHP there are functions rawurlencode and rawurldecode for coding and decoding accordingly. I want otmeteit`, that decoding RNR does{makes} itself if in search the coded parameters have been transferred{handed}. On it I to the law the first chapter{head} of acquaintance c report HTTP. In sledujuchej to the chapter{head} we shall consider construction of searches such as POST (in translation from English - "to send"), that it will be much more interesting, since the given type of searches is used at sending the data from HTML forms. 3. Method POST. In case HTTP of search such as POST exists two variants of transfer of fields from HTML forms, namely, using algorithm application/x-www-form-urlencoded and multipart/form-data. Distinctions between the given algorithms rather essential. The matter is that the algorithm of the first type was created very long time ago when in language HTML yet did not provide an opportunity of a file transfer through HTML the form. So, let's consider these algorithms on examples. 3.1 Content-Type: application/x-www-form-urlencoded. We write the search similar to our search GET for transfer of a login and the password which has been considered in the previous chapter{head}: POST http://www.site.ru/news.html HTTP/1.0rn Host: www.site.rurn Referer: http://www.site.ru/index.htmlrn Cookie: income=1rn Content-Type: application/x-www-form-urlencodedrn Content-Length: 35rn rn login=Petya%20Vasechkin*password=qq Here we see an example of use Content-Type and Content-Length fields of heading. Content-Length speaks, how much the byte will borrow{occupy} area of the data which is separated from heading by one more translation of a line rn. And parameters which earlier for search GET were located in Request-URI, now are in Entity-Body. It is visible, that they are formed precisely also, it is just necessary to write them after heading. I want to note one more important point, nothing prevents, simultaneously with a set of parameters in Entity-Body, to place parameters with other names in Request-URI, for example: POST http: // www.site.ru/news.html? type=user HTTP/1.0rn ..... rn login=Petya%20Vasechkin*password=qq 3.2 Content-Type: multipart/form-data As soon as the Internet has understood the world, that it would be quite good to send through forms also files so W3C the consortium has undertaken completion of format POST of search. By then format MIME (Multipurpose Internet Mail Extensions already enough was widely applied - multi-purpose expansions of the report for formation Mail of messages), therefore, to not invent a bicycle anew, have decided to use a part of the given format of formation of messages for creation POST of searches in report HTTP. What basic differences of this format from type application/x-www-form-urlencoded? The main difference that Entity-Body now it is possible to divide into sections which are divided{shared} by borders (boundary). What is - each section can have own heading for the description of the data which are stored{kept} in him, i.e. in one search it is possible to pass the data of various types (as in Mail the letter you simultaneously with the text can pass files). So, we shall start. We shall consider again all the same example with transfer of a login and the password, but now in a new format. POST http://www.site.ru/news.html HTTP/1.0rn Host: www.site.rurn Referer: http://www.site.ru/index.htmlrn Cookie: income=1rn Content-Type: multipart/form-data; boundary=1BEF0A57BE110FD467Arn Content-Length: 209rn rn - 1BEF0A57BE110FD467Arn Content-Disposition: form-data; name = "login" rn rn Petya Vasechkinrn - 1BEF0A57BE110FD467Arn Content-Disposition: form-data; name = "password" rn rn qqrn - 1BEF0A57BE110FD467A - rn Now let's understand that that is written.:-) I have specially allocated some symbols rn fat that they did not merge with the data. Having looked narrowly it is closely possible to notice a field boundary after Content-Type. This field sets a separator of sections - border. As border the line consisting of latin letters and figures, and as from even some symbols (unfortunately, I I do not remember what else) can be used. In a body of search in the beginning of border it is added ' - ', and the search - border to which symbols ' - ' are added also in the end comes to an end. In our search two units, the first describes a field login, and a field password. Content-Disposition (type of the data in section) speaks the second, what is it there will be data from the form, and in a field name the name of a field is set. On it the heading of the unit comes to an end and further the area of the given units follows, in which value of a field is located (to code value it is not required!). I want to pay your attention that that in headings of sections it is not necessary to use Content-Length, and in heading of search his value is necessary also is in the size of all Entity-Body, worth after the second rn, following for Content-Length: 209rn. I.e. Entity-Body it is separated from heading by additional translation of a line (that it is possible to notice and in sections). And now let's write search for a file transfer. POST http://www.site.ru/postnews.html HTTP/1.0rn Host: www.site.rurn Referer: http://www.site.ru/news.htmlrn Cookie: income=1rn Content-Type: multipart/form-data; boundary=1BEF0A57BE110FD467Arn Content-Length: 491rn rn - 1BEF0A57BE110FD467Arn Content-Disposition: form-data; name = "news_header" rn rn Example novostirn - 1BEF0A57BE110FD467Arn Content-Disposition: form-data; name = "news_file"; filename = "news.txt" rn Content-Type: application/octet-streamrn Content-Transfer-Encoding: binaryrn rn And such news which lays in a file news.txtrn - 1BEF0A57BE110FD467A - rn In the given example in the first section the heading of news is sent, and in the second section the file news.txt is sent. Close{attentive} yes will see fields filename and Content-Type in the second section. The field filename sets a name of a sent file, and field Content-Type - type of the given file. Application/octet-stream speaks about that, what is it the standard dataflow, and Content-Transfer-Encoding: binary speaks on about that, what is it the binary data, nothing coded. Very much an important point. Majority CGI of scripts is written by clever people, therefore they like to check type of the come file which costs{stands} in Content-Type. What for? More often zakachka files on sites it is used for reception of pictures from the visitor. And so, the browser itself tries to define{determine} that for a file the visitor wants to send and inserts corresponding Content-Type into search. The script of it checks at reception, and, for example, if it not gif or not jpeg ignores the given file. Therefore at "manual" formation of search take care of value Content-Type that it was most close to a format of a transmitted file. image/gif For gif image/jpeg For jpeg image/png For png image/tiff For tiff (that is used extremely seldom, a bit too capacious format) In our example the search in which the text file is passed is formed. The search for transfer of a binary file is the same way formed. 4. A postscript. I think, that it is not necessary to tell about transfer of searches about the server in detail. It already business is clean RNR technics{technical equipment}:-). Closely{attentively} enough to read section about functions of job with sockets, or about functions of module CURL in official documentation RNR. From above said, I hope now understandably, why a question: " As to me to generate POST search, using function header? " - bessmyslenen. Function header (string) adds recording only in heading of search, but in any way in a body of search. |
|
|||||||||||||||||||||||||||||