= Week 7, continued :author: Cheng Gong :v: LbDYLUuwnTk [t=0m0s] == Announcements * CS50 Lunch is again this Friday 10/24 at 1:15pm, RSVP at the usual http://cs50.harvard.edu/rsvp. * Jason dressed as a pumpkin one year for his section that happened to land on Halloween 2011 (http://cs50.tv/2011/fall/sections/8/section8.mp4[Section 8] at http://cs50.tv[cs50.tv]), and that year his air pump was working, but by 2012 his costume was deflated. * If you want to join us in pumpkin carving with Daven and Gabe this Friday 10/24, 3pm, RSVP by emailing heads@cs50.harvard.edu. * The final project will be discussed soon, but they can be almost any project of interest to you, with the approval of your teaching fellow. * To help you with that, we will have seminars, optional classes taught by teaching fellows and other staff from across campus, on various topics that are fun and different. This year's lineup: ** 3D Modeling and Manufacture ** Amazing Web Apps with Ruby on Rails ** Android 101 ** Breaking Through The (Google) Glass Ceiling ** Build Tomorrow's Library ** Cloud Computing with Amazon Web Services (AWS) ** CSS: Awesome Style and Design ** Data Analysis in R ** Data Visualization and D3 ** Essential Scale-Out Computing ** Exposing Digital Photography ** How to Build Innovative Technologies ** iOS App Development with Swift ** Learning iOS: Create your own app with Objective-C! ** Light Your World (with Hue Bulbs) ** Meteor: a better way to build apps ** Teach Your Computer to See: Augmented Reality with OpenCV ** Transitioning to Agile Development from Waterfall * If you want to register or see more information about these topics, visit http://cs50.harvard.edu/register. [t=3m14s] == HTTP Review * We began and concluded on Monday with HTTP, Hypertext Transfer Protocol, which is just the way your web browser speaks to a web server, like the way humans extend a hand to each other to make a handshake. * A protocol is just a set of conventions of sending information back and forth, like this familiar picture: + * The most common method used is *GET*, that looks like this: + [source] ---- GET / HTTP/1.1 Host: www.google.com ... ---- * And that request is placed inside a digital envelope with the IP address of the computer it is from, and the IP address of the recipient. But we need one more piece of information, the port number, since the server might be listening for different kinds of communication. Port 80 is the most common as it's the default for web traffic, so we'll see that often, along with 443, used for secure web traffic (HTTPS). * Also remember that the `/` after `GET` means that we want the root of the web server, or the default webpage. * Hopefully the server responds with something like this: + [source] ---- HTTP/1.1 200 OK Content-Type: text/html ... ---- * And the number 200 is a convention saying that everything okay, with the `Content-Type` of the message `text/html`, and the `...` will be the webpage itself. * So now we can pick up by writing HTML, Hypertext Markup Language, that we'll use to write webpages and specify their structure and style. [t=5m50s] == HTML * On Monday we looked at this: + [source, html] ---- hello, world hello, world ---- ** Notice the angle brackets and the words between those brackets, which we'll start calling *tags*. So `` and `` are the open and close tags, or the start and end tags, of an HTML *element* called head. The same applies to `` and `` and so forth. * We'll look at more elements as we need them. * Browsers interpret HTML pretty straightforwardly. When we say `` that just means "here's the start of our HTML page", `` means "this is the start of the head section", `` means "this is it for the head section, stand by for something else", and so forth. * Text that isn't in a tag, like `hello, world`, will just be displayed in the screen. * Notice the indentation, where we indent every time a new tag is opened, and unindent when it is closed, just like curly braces. ** With ``, we used our judgment in keeping it on the same line since it looks cleaner, fits easily in one line, and only contains one inner element. * The indentation could also help us think of the webpage as a tree: + image::dom.png[alt="Graphical representation of DOM as a tree", width=400] ** The `html` tag is like the root of the tree. ** We'll call this tree the *Document Object Model* (*DOM*) that represents the HTML. ** `html` has two children, with `head` on the left and `body` on the right, and `head` has one child, `title`, which itself has a child, `hello, world`. The oval conveys that it's not a tag or element, but just text. These are all just arbitrary conventions that we use to represent an HTML document in a tree like this. ** And as another note, `<!DOCTYPE html>` in our original source code is not a tag, but rather just a line placed there to indicate to the browser that this is an HTML5 file (different versions have different http://en.wikipedia.org/wiki/Document_type_declaration[declarations]). * Let's open `hello.html` with `gedit` in the appliance: + [source, html] ---- <!DOCTYPE html> <html> <head> <title>hello hello, world ---- ** This is a webpage that lives on the Desktop (or wherever you've saved it) and can be opened in Chrome with control-o: + image::hello-file.png[alt="hello.html opened as a file", width=500] ** Notice that the URL in the address bar reads `pass:[file:///home/jharvard/Desktop/hello.html]`, indicating that we are looking at a file on our local hard drive, and no one else should be able to see it. [t=11m41s] == Servers and Permissions * We can fix this by using a web server. The CS50 Appliance, apart from being able to compile and run C code in a standard environment, has also been configured to run standard, open-source server software, including Apache (the most popular web server software in the world) and MySQL (a database software we'll get to). Basically, we can use the appliance as a web server. * First, we open our Terminal, and run the following: + [source] ---- jharvard@appliance (~): cd Desktop/ jharvard@appliance (~/Desktop): ls hello.html jharvard@appliance (~/Desktop): mv hello.html ../vhosts/localhost/public/ ---- ** The last command moves our file `hello.html` into a folder in our home directory. ** `vhosts` is the root folder that the appliance's server software looks into, `localhost` literally just means "link:http://en.wikipedia.org/wiki/Localhost[this computer]," and `public` means that everything within that folder is public. * We can check that it was indeed moved successfully with: + [source] ---- jharvard@appliance (~/Desktop): cd ../vhosts/localhost/public/ jharvard@appliance (~/vhosts/localhost/public/): ls hello.html ---- * And now we can open a new tab in Chrome and enter `pass:[http://localhost/hello.html]`, meaning we're visiting our own computer, and requesting the file `hello.html`. * If we compare what we see now with to we saw earlier, the page looks the same, with the same "hello, world" text. But the difference now is that HTTP is being used. * In the bottom right corner of the appliance, we have something that looks like this: + image::ip.png[alt="IP address displayed in the CS50 Appliance", width=300] * This is a private IP address, so only the local network can access it. And it's the IP address of David's appliance, so yours might be different. * We can open the version of Chrome on our Mac (that's running the appliance), go to `pass:[http://172.16.254.133/hello.html]`, and see our webpage from our Mac. The web server doesn't have an easy-to-remember name, and isn't accessible by the rest of the Internet, but HTTP is indeed being used by Chrome on our Mac to communicate with the server running on our appliance. * Let's go back to our appliance and `gedit`, and copy all the files from today's http://cdn.cs50.net/2014/fall/lectures/7/w/src7w/[source code] to the `public` folder, including `cat.jpg`, and change `hello.html` to look like this: + [source, html] ---- hello cat.jpg ---- ** But when we save and reload, all we see is this: + image::cat-jpg.png[alt="cat.jpg displayed as text", width=500] * We need another tag to tell the browser what we want to do: + [source, html] ---- hello ---- ** Note that, since the `img` tag is always going to be empty, it has the special syntax of being closed with a `/>` at the very end. * But when we reload, we see something like this: + image::cat-broken.png[alt="cat.jpg not displayed", width=500] * We can go to *View*, *Developer*, *Developer Tools*: + image::developer-tools.png[alt="Developer tools in Chrome", width=700] * And if we go to the *Network* tab and reload the page, we can look at the requests being made: + image::cat-403.png[alt="cat.jpg with 403 response code", width=700] * In particular, `hello.html` has a response of *200 OK*, but `cat.jpg` has *403 Forbidden*. (Recall that a *404* is returned when the file isn't found, so we know `cat.jpg` exists.) * We can go back in our appliance, and run `ls -l` (bolded), which lists the files with more details: + [source, subs=quotes] ---- jharvard@appliance (~/vhosts/localhost/public): ls cat.jpg css-1.html css-2.html hello.html link.html logo.gif search-0.html search-2.html search-3.html table.html css-0.html css-2.css headings.html image.html list.html paragraphs.html search-1.html search-3.css search-4.html jharvard@appliance (~/vhosts/localhost/public): *ls -l* total 212 *-rw------- 1 jharvard students 133986 Oct 22 13:26 cat.jpg* -rw------- 1 jharvard students 619 Oct 22 13:26 css-0.html -rw------- 1 jharvard students 862 Oct 22 13:26 css-1.html -rw------- 1 jharvard students 155 Oct 22 13:26 css-2.css -rw------- 1 jharvard students 525 Oct 22 13:26 css-2.html -rw------- 1 jharvard students 368 Oct 22 13:26 headings.html *-rw-r--r-- 1 jharvard students 143 Oct 22 13:27 hello.html* -rw------- 1 jharvard students 311 Oct 22 10:44 image.html -rw------- 1 jharvard students 261 Oct 22 13:26 link.html -rw------- 1 jharvard students 341 Oct 22 13:26 list.html -rw------- 1 jharvard students 11988 Oct 22 13:26 logo.gif -rw------- 1 jharvard students 1788 Oct 22 13:26 paragraphs.html -rw------- 1 jharvard students 431 Oct 22 13:26 search-0.html -rw------- 1 jharvard students 458 Oct 22 13:26 search-1.html -rw------- 1 jharvard students 541 Oct 22 13:26 search-2.html -rw------- 1 jharvard students 33 Oct 22 13:26 search-3.css -rw------- 1 jharvard students 477 Oct 22 13:26 search-3.html -rw------- 1 jharvard students 1126 Oct 22 13:26 search-4.html -rw------- 1 jharvard students 714 Oct 22 13:26 table.html ---- ** We see `hello.html` and `cat.jpg`, but there are two ``r``s for `hello.html` that `cat.jpg` doesn't (bolded). * It turns out that we need to change the *mode*, or permissions, of the file, to allow the world to read (view) `cat.jpg`. * For `hello.html`, the ``r``s are what allow everyone to read it. So we can run *`chmod`*, which means "change mode," with `a+r`, which means "all+read," allowing everyone to read, `cat.jpg`: + [source] ---- chmod a+r cat.jpg ---- * Now if we run `ls -l` (bolded), we see that the permissions have been changed: + [source, subs=quotes] ---- jharvard@appliance (~/vhosts/localhost/public): *ls -l* total 212 *-rw-r--r-- 1 jharvard students 133986 Oct 22 13:26 cat.jpg* -rw------- 1 jharvard students 619 Oct 22 13:26 css-0.html -rw------- 1 jharvard students 862 Oct 22 13:26 css-1.html -rw------- 1 jharvard students 155 Oct 22 13:26 css-2.css -rw------- 1 jharvard students 525 Oct 22 13:26 css-2.html -rw------- 1 jharvard students 368 Oct 22 13:26 headings.html *-rw-r--r-- 1 jharvard students 143 Oct 22 13:27 hello.html* -rw------- 1 jharvard students 311 Oct 22 10:44 image.html -rw------- 1 jharvard students 261 Oct 22 13:26 link.html -rw------- 1 jharvard students 341 Oct 22 13:26 list.html -rw------- 1 jharvard students 11988 Oct 22 13:26 logo.gif -rw------- 1 jharvard students 1788 Oct 22 13:26 paragraphs.html -rw------- 1 jharvard students 431 Oct 22 13:26 search-0.html -rw------- 1 jharvard students 458 Oct 22 13:26 search-1.html -rw------- 1 jharvard students 541 Oct 22 13:26 search-2.html -rw------- 1 jharvard students 33 Oct 22 13:26 search-3.css -rw------- 1 jharvard students 477 Oct 22 13:26 search-3.html -rw------- 1 jharvard students 1126 Oct 22 13:26 search-4.html -rw------- 1 jharvard students 714 Oct 22 13:26 table.html ---- * We can also do the opposite with `chmod a-r cat.jpg` if we want to take that permission back. * But let's not do that, and instead go back to our Chrome window, and if we reload the page, now we are able to see our grumpy cat. [t=20m55s] == Links * Let's try some other things. If we want a link, we could write something like this: + [source, html] ---- hello search for cats ---- ** `a` opens the tag and is short for "anchor", `href` stands for "hypertext reference", and `href` is not a tag but *attribute*, or something that can modify the behavior of a tag. In this case, `href` will tell the anchor tag the address that it should link to when it's clicked. The text in between, `search for cats`, is what will be shown to the human. * We can see this if we save and reload: + image::cat-link.png[alt="link to search for cats", width=500] ** And notice that we can be tricky by making the link go to somewhere else: + [source, html] ---- hello http://google.com/ ---- * Notice that now the link looks like it goes to `http://google.com/`, but only when we hover over it do we see that it doesn't: + image::badguy.png[alt="Link going to badguy.com", width=500] * This is why you should never, ever click links in emails, because it's really easy to trick someone. (As an aside, someone actually bought the domain `http://www.bankofthevvest.com`, which looks very much like `http://www.bankofthewest.com` at first glance, in small font in an email, to try to trick people. * In fact, if we were to go to `http://www.bankofthewest.com`, we can click *View*, *Developer*, *View Source*, and copy and paste the code we see into our own page: + image::botw-source.png[alt="Bank of the West source code", width=700] * It's not quite this easy, but we see that we are making progress toward making our own banking website: + image::botw-hello.png[alt="Bank of the West source code in hello.html", width=700] * The other files that the website might be using, include CSS files or images, probably need to be downloaded for the website to be complete. [t=25m16s] == Lists, Paragraphs, Tables * Let's look now at http://cdn.cs50.net/2014/fall/lectures/7/w/src7w/list.html[`list.html`]: + [source, html] ---- list ---- ** The `