Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting HTTP.

Similar presentations


Presentation on theme: "CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting HTTP."— Presentation transcript:

1 CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting HTTP

2 CIT 383: Administrative Scripting Topics 1.HTTP 2.URLs 3.Cookies 4.Base64

3 CIT 383: Administrative Scripting Web Client/Server Interaction HTTP Request (form submission)‏ HTTP Response (new web page)‏ Server processingUser waits HTTP Request (form submission)‏ User interaction HTTP Response (new web page)‏ User waits Server processing Browser Server

4 CIT 383: Administrative ScriptingSlide #4 HTTP: HyperText Transfer Protocol Simple request/respond protocol –Request methods: GET, POST, HEAD, etc. –Protocol versions: 1.0, 1.1 Stateless –Each request independent of previous requests, i.e. request #2 doesn’t know you auth’d in #1. –Applications responsible for handling state.

5 CIT 383: Administrative ScriptingSlide #5 HTTP Request GET http://www.google.com/ HTTP/1.1 Host: www.google.com User-Agent: Mozilla/5.0 (Windows NT 5.1) Gecko/20060909 Firefox/1.5.0.7 Accept: text/html, image/png, */* Accept-Language: en-us,en;q=0.5 Cookie: rememberme=true; PREF=ID=21039ab4bbc49153:FF=4 MethodURL Protocol Version Headers Blank Line No Data for GET method

6 CIT 383: Administrative ScriptingSlide #6 HTTP Response HTTP/1.1 200 OK Cache-Control: private Content-Type: text/html Server: GWS/2.1 Date: Fri, 13 Oct 2006 03:16:30 GMT... (page data)... Protocol VersionHTTP Response Code Headers Blank Line Web Page Data

7 CIT 383: Administrative Scripting HTTP Methods HEAD Same as GET, but only asks for headers, not body. GET Requests a representation of the resource. Most common method. Should not cause server to modify (write, delete) any resources. POST Submits data to be processed to the resource. The data is included in the body of the request. This may result in the creation of a new resource or the updates of existing resources or both. PUT Uploads a representation of the specified resource. DELETE Deletes the specified resource. TRACE Echoes back the received request, so that a client can see what intermediate servers are adding or changing in the request.

8 CIT 383: Administrative Scripting HTTP Request Headers HeaderDescriptionExample AcceptAcceptable content types.Accept: text/plain AuthorizationHTTP authentication credentials. Authorization: Basic QWxhZGRpbjpvcGVuIHNl c2FtZQ== Cache-ControlCaching directivesCache-Control: no cache CookieCookie data for server.Cookie: color=red DateDate and time sentDate: 29 Oct 2008 1:02:03 HostName of serverHost: cs.nku.edu If-Modified- Since Allows a 304 Not Modified to be returned for caching. If-Modified-Since: 29 Oct 2008 1:02:03 GMT User-AgentBrowser description stringMozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.2) Ubuntu/8.04 Firefox/3.1

9 CIT 383: Administrative Scripting HTTP Response Headers HeaderDescriptionExample Cache-ControlCaching directivesCache-Control: no cache Content- Encoding Type of encoding used.Content-Encoding: gzipSer Content-LengthLength of data returned.Content-Length: 1024 Content-TypeType of data returned.Content-Type: text/html DateDate and time response sent.Date: 29 Oct 2008 1:02:03 ExpiresDate after which data expired.Expires: 1 Nov 2008 1:02:03 LocationUsed in redirectionLocation: http://www.example.com/abo ut/ ServerServer identification string.Server: Apache/2.0.55 Set-CookieCookie created by server.Set-Cookie: color=red

10 CIT 383: Administrative Scripting HTTP Response Codes CodeDescriptionMeaning 200OKStandard success response. 201CreatedNew resource created. 301Moved permanentlyPermanent redirect to new URI. 304Not modifiedSafe to use page stored in cache. 307Temporary redirectUse new URI now; try old later. 401UnauthorizedAuthentication failed. 403ForbiddenDisallowed, auth will not help. 404Not foundResource was not found. 405Method not allowedUsed GET when should use POST. 500Internal server errorInternal server error.

11 CIT 383: Administrative Scripting Net::HTTP Class Net::HTTP.get(host, path): returns resource from host, path as a string. Net::HTTP.get_response(host, path): returns HTTP response object, includes body + headers. Net::HTTP.post_form(host, path,{parameters}): returns resource from host, path as a string using POST instead of GET, sending form parameters as a hash.

12 CIT 383: Administrative Scripting Redirection Example def fetch(uri) response = Net::HTTP.get_response(uri) case response when Net::HTTPSuccess then response when Net::HTTPRedirection then fetch(response['location'], limit-1) else response.error! end

13 CIT 383: Administrative Scripting URI Format :// @ : / ? –Whitespace marks end of URL –“@” separates userinfo from host –“?” marks beginning of query string –“&” separates query parameters –%HH represents character with hex values –ex: %20 represents a space http://username:password@www.auth.com:8001/a%20spaced%20path

14 CIT 383: Administrative Scripting URI Class URI.extract(string): returns array of URI strings extracted from string. URI.extract("text http://example.com/ and mailto:test@example.com and text here also.") => ["http://example.com/", "mailto:test@example.com"] URI.join(string,string,...): joins two or more strings into a URI. URI.parse(string): creates URI object f/ string. URI.split(uri): splits URI string into protocol, host, path, query, etc. components.

15 CIT 383: Administrative ScriptingSlide #15 Cookies Server to Client Content-type: text/html Set-Cookie: foo=bar; path=/; expires Fri, 20-Feb- 2004 23:59:00 GMT Client to Server Content-type: text/html Cookie: foo=bar

16 CIT 383: Administrative Scripting Base64 Encoding How do you send binary data using text? –Email attachments (MIME). –Cookies (HTTP). Base64: encode 3 bytes as 4 text characters –Use characters A-Za-z0-9+/ to store 6 bits of data. –Byte has 8 bits, so 3 bytes = 24 bits –4 base64 chars (6 bits each) = 24 bits –Use = to pad output if input not multiple of 3 bytes.

17 CIT 383: Administrative Scripting Base64 Class encode = Base64.encode64(‘informatics‘) decode = Base64.decode64(‘aW5mb3JtYXRpY3M=‘)

18 CIT 383: Administrative ScriptingSlide #18 References 1.Michael Fitzgerald, Learning Ruby, O’Reilly, 2008. 2.David Flanagan and Yukihiro Matsumoto, The Ruby Programming Language, O’Reilly, 2008. 3.Hal Fulton, The Ruby Way, 2 nd edition, Addison- Wesley, 2007. 4.Robert C. Martin, Clean Code, Prentice Hall, 2008. 5.Dave Thomas with Chad Fowler and Andy Hunt, Programming Ruby, 2 nd edition, Pragmatic Programmers, 2005.


Download ppt "CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting HTTP."

Similar presentations


Ads by Google