THE WORLD WIDE WEB**

Image

The previous chapter described the internet, the globally connected set of computer networks that share a suite of protocols. The World Wide Web is a system built on top of the internet, one so popular that it is often confused with the internet itself. In this chapter, we dive into the details of the web. We first look at its key attributes and related programming languages, and then we look at web browsers and web servers.

Overview of the World Wide Web

The World Wide Web , often just called the web , is a set of resources, delivered using HyperText Transfer Protocol (HTTP) over the internet. A web resource is anything that can be accessed using the web, such as a document or an image. A computer or software program that hosts web resources is called a web server , and a web browser is a type of application commonly used to access content on the web. Browsers are used to view documents known as web pages , and a collection of related web pages is known as a website . The web is distributed, addressable, and linked. Let’s begin by examining each of those core attributes.

The Distributed Web

The World Wide Web is distributed . No centralized organization or system governs what content can be published to the web. Any computer connected to the internet can run a web server, and the owner of such a computer can make available any content they wish. That said, organizations or countries may choose to block users from accessing certain content on the web, and governments can shut down websites that host illegal content. Aside from those cases, the web is an open platform for publishing whatever people wish to publish, and no single organization controls what content is made available.

The Addressable Web

The web uses Uniform Resource Locators (URLs) to give every resource on the web a unique address that includes both its location and how to access it. URLs are commonly referred to as web addresses or just addresses . To illustrate how these addresses are structured, let’s use a URL for a fictitious travel website, as shown in Figure 12-1 . This URL identifies a page with information about traveling to the Carolinas.

image

Figure 12-1: An example URL

A URL is composed of multiple parts. The URL scheme identifies the application layer protocol for accessing the resource. In this case, the protocol is HTTP, which we’ll cover in more detail later. The colon (:) character indicates the end of the scheme portion. Following two forward slashes (//) is the authority portion of the URL. In this example, the authority portion contains a DNS hostname of the server(s) where the resource resides, travel.example.com . An IP address can be used here as well. Other information besides the host can also go in this section, such as a username (preceding the host and followed by an @ sign) or port number (following the host and prefixed with a colon). The path portion of the URL is next; it specifies the location of a resource on the web server. A URL path is analogous to a filesystem path, organizing resources into a logical hierarchy. In our example, the path /destinations/carolinas implies that the site has a collection of pages that describe travel destinations, and the particular page specified in the URL is a page about the Carolinas. We could reasonably assume that if the site had a page describing Florida as a destination, it would be found at /destinations/florida . Finally, the query portion of the URL acts as a modifier to the resource returned to the client. In our example, the query indicates that the carolinas page should display locations at the beach. The format and meaning of the query portion of the URL varies from site to site.

A lot of information is packed into that URL, so let me restate how to read it in plain language. A website is running on a computer named travel.example.com . The site speaks HTTP, so use that protocol when connecting to the site. On that site there’s a page called carolinas , part of a collection of destinations . The query string directs the page to only show locations that are at the beach.

A URL does not have to include every element in the example in Figure 12-1 . It may also contain some elements not included in this example. A URL that includes only the scheme and the authority is perfectly valid, such as http://travel.example.com . In that scenario, the website serves its default page, since no path is provided.

EXERCISE 12-1: IDENTIFY THE PARTS OF A URL

For the following URLs, identify the scheme, username, host, port, path, and query. Not all URLs include all these parts.

You can check your answers in Appendix A .

A web browser typically displays the current URL in its address bar, as illustrated in Figure 12-2 .

image

Figure 12-2: The address bar

Today, it’s common for browsers to exclude the scheme, colon, and forward slashes in the address bar representation of the URL. This doesn’t mean these elements of the URL are no longer used by the browser. The browser is just trying to simplify things for users. The specifics of how the URL is displayed continue to change over time, with various browsers behaving differently.

Figure 12-3 shows examples of how Google Chrome (version 77) displays URLs in its address bar.

image

Figure 12-3: Chrome’s address bar

The top image in Figure 12-3 shows the address bar when an HTTP site is loaded. Chrome doesn’t display the http:// prefix in its address bar. Note the Not secure text. The lower image shows the address bar when an HTTPS site is loaded. HTTPS is the secure version of HTTP. Chrome omits the https:// prefix but displays a padlock icon indicating this is an HTTPS site.

We’ve been discussing URLs in the context of web pages, but URLs extend to other resources on the web too. An image shown on a web page, for example, has its own URL, as does a script file or an XML data file. A web browser only shows the URL of the web page in its address bar, but a typical web page references various other resources by URL; the browser automatically loads those resources.

Sometimes it isn’t necessary to include the scheme, hostname, or even the full path in a URL. When a URL omits one or more of these elements, it’s known as a relative URL . A relative URL is interpreted as relative to the context in which it’s found. For example, if a URL like /images/cat.jpg is used on a web page, the browser that loads the page assumes that the scheme and hostname of the cat photo match the scheme and hostname of the page itself.

The Linked Web

The nature of URLs, where every resource on the web has a unique address, makes it easy for one web resource to reference another. A reference from one web document to another is known as a hyperlink , or just a link . Such links are one-way; any web page can link to another page without permission or a reciprocal link. This system of pages linking to one another is what puts the “web” in World Wide Web. Documents like web pages that can be connected with hyperlinks are known as hypertext documents.

The Protocols of the Web

The web is delivered using HyperText Transfer Protocol (HTTP) , and its secure variant, HTTPS .

HTTP

Despite its name, HTTP isn’t just for transferring hypertext; it’s used for reading, creating, updating, and deleting all resources on the web. HTTP typically relies on TCP/IP. TCP ensures that data is reliably transferred, and IP handles host addressing. HTTP itself is based on a model of request and response . An HTTP request is sent to a web server, and the server replies with a response.

Each HTTP request includes an HTTP method , also informally called an HTTP verb , that describes what kind of action the client is requesting of the server.

Some commonly used HTTP methods:

GET    Retrieve a resource without modifying it.

PUT    Create or modify a resource at a specific URL on the server.

POST    Create a resource on the server, as a child of an existing URL.

DELETE    Remove a resource from the server.

Any HTTP method can be attempted on any resource, but the server hosting a specific resource often won’t allow some methods on that resource. For example, most websites do not allow clients to delete resources. Those that do allow deletion almost always require the user to log on with an account that has permission to delete content.

The most commonly used method on a typical website is GET . When a web browser navigates to a website, the browser performs a GET on the requested page. That page may include references to scripts, images, and so forth, and the browser then also uses the GET method to obtain those resources before the page can be fully displayed.

Each HTTP response includes an HTTP status code that describes the server’s response. Each status code is a 3-digit number, where the most significant digit indicates the general class of response. Responses in the 100 range are informational. Responses in the 200 range indicate success. Responses in the 300 range indicate redirection. Responses in the 400s indicate an error on the client side—the request wasn’t properly formed by the client. A 500 range response means the server encountered an error.

Some commonly used HTTP status codes:

200    Success. The server was able to fulfill the request.

301    Moved Permanently. The browser should redirect the request to a different URL, specified in the response.

401    Unauthorized. Authentication is required.

403    Forbidden. The user doesn’t have access to the requested resource.

404    Not Found. The server didn’t find the requested resource.

500    Internal Server Error. Something unexpected happened on the server.

HTTP is fairly easy to understand. It uses human-readable text to describe requests and responses. The first line of a request includes an HTTP method, the URL of the resource, and the requested version of HTTP. Here’s an example:

GET /documents/hello.txt HTTP/1.1

This simply means that the client is asking the server to send it the content of /documents/hello.txt using HTTP version 1.1 . Following the request line, an HTTP request usually includes header fields that provide more information about the request and an optional message body.

Similarly, an HTTP response uses a simple text format. The first line includes a version of HTTP, a status code, and a response phrase. Here’s an example of the first line of an HTTP response:

HTTP/1.1 200 OK

In this example response, the server is indicating a status code of 200 and a response phrase of OK . Just like HTTP requests, responses may also include header values and a message body. Figure 12-4 provides a more detailed, but still simplified, example of an HTTP request and response.

image

Figure 12-4: A simplified HTTP request and response

NOTE

Please see Project #36 on page 283 , where you can look at HTTP network traffic .

HTTPS

A secure variation of HTTP known as HTTPS (HyperText Transfer Protocol Secure) is commonly used on the web to encrypt data sent over the internet. Encryption is the process of encoding data into a format that’s unreadable. Decryption is the reversal of encryption, making encrypted data readable again. Cryptographic algorithms encrypt and decrypt data using a secret sequence of bytes known as a cryptographic key . Because keys can be kept secret, the algorithm itself can be well-known.

HTTPS uses two kinds of encryption. Symmetric encryption uses a single shared key both for encrypting and decrypting a message. Asymmetric encryption uses two keys (a key pair ): a public key is used to encrypt data, and a private key is used to decrypt data. Asymmetric encryption allows a public key to be shared freely so that anyone can encrypt and send data, whereas a private key is shared only with trusted parties who need to be able to receive and decrypt the data.

Without HTTPS, web traffic is transmitted “in the clear,” meaning it is unencrypted and can be intercepted or modified in transit by malicious parties. HTTPS helps to reduce these risks. With HTTPS, the entire HTTP request is encrypted, including the URL, headers, and body. The same is true of an HTTPS response; it’s fully encrypted. HTTPS takes an HTTP request and encrypts it using a protocol called Transport Layer Security (TLS) . In the past, a similar protocol called Secure Sockets Layer (SSL) was used, but due to security issues, it has since been deprecated in favor of TLS. When we speak of HTTPS, what we mean is HTTP encrypted with TLS.

When an HTTPS session begins, the client connects to the server with a client hello message with details of how it wishes to securely communicate. The server responds with a server hello message that confirms how the communication will occur. The server also sends a set of bytes known as a digital certificate , which includes the server’s public cryptographic key, used for asymmetric encryption. The client then checks if the server’s certificate is valid. If so, the client encrypts a string of bytes using the server’s public key and then sends the encrypted message to the server. The server decrypts the bytes using its private key. The server and client both use the information previously exchanged to compute a shared secret key, used for symmetric encryption. Once both client and server have the shared key, that key is used to encrypt and decrypt all communication between the client and server for the duration of the session.

HTTPS was previously only used in limited cases, for websites that dealt with particularly sensitive information. However, the web is moving to a state in which HTTPS is the norm rather than the exception. There is a growing belief that the security and privacy benefits of HTTPS make sense for most, if not all, traffic on the web. Google has encouraged this change, by marking HTTP sites as “Not secure” in Chrome and by using the presence of HTTPS as a positive signal to its search engine, helping to boost the Google search rank of HTTPS sites.

NOTE

Please see Project #37 on page 285 , where you can set up a simple web server on your local network.

The Searchable Web

For many people, the typical entry point to the web is a search. Rather than navigate to a particular URL, a user types some search terms into their browser and sees what comes up. Browser design encourages this, since browsers commonly leverage the address bar as a search box too. Even when a user wants to visit a particular site, they often perform a search for that site, then click the resulting link, rather than entering the full URL in the address bar. This is a design that enhances usability of the browser, even as it blurs the distinction between URLs and search terms, browser and search engine.

Despite the prevalence and usefulness of searching the web, the capability of searching isn’t a native feature of the web. There isn’t a standard specification for how searching should work. This means that searching, one of the key features of the web, relies on nonstandard, proprietary search engines. At the time of this writing, Google dominates the web searching space, and although there are good alternative search engines, their worldwide usage is a fraction of Google’s.

The Languages of the Web

Any content that can be saved as a file can be hosted on the web. For example, a web server can host a collection of Excel files, and they can be downloaded from the website and opened in Excel. However, a web browser is much more than just a tool for downloading files to be opened in other applications. A web browser not only downloads content, but also renders web pages. These pages can be simple documents, or interactive web applications. To make this possible, browsers understand three computer languages, which are used to construct websites.

HyperText Markup Language (HTML)    Defines the structure of a web page. In other words, it defines what is on the page . For example, HTML can specify that a button exists on a web page.

Cascading Style Sheets (CSS)    Defines the appearance of a web page. In other words, it defines how the page looks . For example, CSS can specify that the aforementioned button is 30 pixels wide and blue.

JavaScript    Defines the behavior of a web page. In other words, it defines how the page functions . For example, JavaScript can be used to add two numbers together when a button is clicked.

These three languages are used together to create the content of the web. It’s worth noting that web browsers are also capable of rendering some other data types too, notably certain image, video, and audio formats, but we won’t go into those in detail. Let’s now dive into each of the three foundational languages of the web: HTML, CSS, and JavaScript.

Structuring the Web with HTML

HTML is a markup language that describes the structure of a web page. Note that HTML isn’t a programming language. A programming language describes operations that a computer should perform, whereas a markup language describes the structure of data. In the case of HTML, the data in question represents a web page. A web page can contain various elements such as paragraphs, headings, and images. Here’s an example of a simple web page described in HTML.

<!DOCTYPE html>

            A Cat           

Thoughts on a Cat

    

This is a cat.

    cat photo   

You see a number of items enclosed in less than ( < ) and greater than ( > ) signs. These are known as HTML tags , sets of text characters used to define the parts of an HTML document. As an example, the tag used to indicate the start of a paragraph is <p> . A corresponding tag is used to indicate the end of a paragraph, </p> . Note the slash in the end tag, differentiating it from the start tag. An HTML element is a portion of the page beginning with a start tag, ending with an end tag, and including the content between the tags. For example, this is an HTML element: <p>This is a cat.</p> . In truth, not all elements require an end tag. For example, the img element, used for representing an image, needs no end tag. You can see this in the previous HTML code example.

Figure 12-5 shows how the example HTML might be rendered in a web browser.

image

Figure 12-5: Our example web page rendered in a web browser

The document intentionally included no information about how it should be presented, so a browser uses a default font and size for the heading and paragraph. In this example, the browser also defaulted to black text on a white background—again, that wasn’t specified in the document. Since this HTML example contains no style information, different browsers can choose to render this page slightly differently.

Let’s examine the HTML code example more closely. The first line of an HTML document declares that the file is an HTML document, like so: <!DOCTYPE html> . After that, an HTML document is structured as a tree, with parent elements and child elements. The <html> tag is the top-level parent tag; everything is enclosed between <html> and </html> . You can interpret those two tags as “HTML starts here” and “HTML ends here.” This makes sense—everything in an HTML document should be HTML! The <html> tag also contains an attribute called lang that identifies the language of this document as en , the code for English.

The html element has two child elements: head and body . Elements contained in the head (between <head> and </head> ) describe the document, whereas elements in the body (between <body> and </body> ) make up the contents of the document.

In our example, the head contains two elements: a meta element describing the character set used to encode our document, and a title . Browsers typically show the title text on a page’s tab and use it as a default name when a user adds a bookmark or favorite. Search engines use the title text when showing results. For those reasons it’s important for web developers to give meaningful titles to their pages.

The body in our example includes an <h1> tag, which is used for a heading element. Heading tags of <h1> through <h6> are available, with h1 intended to be used as the highest level of section headings, and h6 as the lowest level. A paragraph follows the heading, indicated with a <p> tag, and after that an image is included using <img> . Note that the bytes of the image itself aren’t present in the HTML. Instead, the <img> tag simply references an image file by a relative URL ( cat.jpg ). To fully load this page, a browser needs to make a separate HTTP request to download the image. In this example, the image URL is simply a filename, meaning it’s hosted on the same server and in the same path as the document itself. If the image were hosted elsewhere, a URL with a path or a server name could be used. The <img> tag also has an alt attribute, which provides alternate text that describes the image. This is used in cases in which the image cannot be rendered, such as when a text-only browser or a screen reader that reads the contents of the page aloud is being used.

You may have noticed that the earlier HTML code used indentation to show the nesting of various elements on the page. For example, the <h1> and <p> tags are indented to the same level, showing that they are child elements of the <body> tag. This is a common practice in web development to improve readability of HTML, but it’s not required. In fact, whitespace beyond a single space or tab doesn’t matter in an HTML document! We could remove all the extra spaces, tabs, and line breaks, leaving us with the HTML all on a single line, and the document would render the same way in a browser. Web browsers ignore extra whitespace, so spacing out elements on a page is only helpful to developers.

NOTE

Please see Project #38 on page 287 , where you can have your local website return a document structured with HTML rather than simple text .

The HTML elements we’ve covered so far are only a small percentage of the total elements recognized by web browsers. We won’t exhaustively cover all of HTML here; it is well documented online. The specifications for HTML were previously maintained by two organizations: the World Wide Web Consortium (W3C) and the Web Hypertext Application Technology Working Group (WHATWG). The last major version of HTML to receive “Recommendation” status from the W3C was HTML5 . In 2019, the two organizations agreed that ongoing development of the HTML standard will be handled principally by the WHATWG, in what is known as the HTML Living Standard , which is continually maintained.

Modern browsers attempt to support both current and older versions of HTML, since plenty of web content was authored with earlier HTML standards in mind. In the past, browsers introduced nonstandard HTML elements, some of which eventually became standardized, while others fell out of use and lost support. Web browser developers must balance innovation with adherence to standards, while still supporting less-than-perfect HTML that’s sometimes found on the web. Web browsers are ever evolving, and different browsers sometimes render the same content differently. This means that web developers regularly test their creations on multiple browsers to ensure consistent behavior.

Styling the Web with CSS

In our earlier example HTML, we used tags that described the structure of a document, but those tags did not convey any information about how the document should be presented. This was intentional; we want to keep structure and style separate. A division between the two allows for the same content to be rendered with different styles in different contexts. For example, most web content should be rendered differently on a large PC screen versus a small mobile screen.

Cascading Style Sheets (CSS) is the language used to describe the style of a web page. A style sheet is a list of rules. Each rule describes a style that should be applied to a certain part of the page. Each rule includes a selector, which indicates what elements on the page should have the style applied. The cascading term refers to the ability for multiple rules to apply to the same element. Let’s look at a simple example:

p {
   font-family: Arial, Helvetica, sans-serif;
   font-size: 11pt;
   margin-left: 10px;
   color: DimGray;
}
 
h1 {
   font-family: 'Courier New', Courier, monospace;
   font-size: 18pt;
   font-weight: bold;
}

In this example, style rules are defined for paragraph ( p ) elements and heading 1 ( h1 ) elements. When this CSS is applied to a page, all paragraphs on that page use the specified font, with a size of 11 point, a left margin of 10 pixels, and gray text. Similarly, h1 headings use the specified bolded font with a size of 18 point. Note that font-family is a list of fonts, not just a single font. This means that a web browser should try to find a matching font, starting with the leftmost font and proceeding to the right until a font match is found. Not every client device has the first choice of font installed; specifying multiple fonts increases the chance that a matching font will be available.

You can apply a style sheet to a web page in a couple of ways. One option is to include the CSS rules within a style element on the page. For example:

This isn’t ideal, because the style and structure are now closely related. A better option is to specify the CSS rules in a separate file, also hosted on the web. This approach keeps our HTML and CSS completely separate, and it allows multiple HTML files to use the same style sheet. This way we can change a CSS rule, and it will apply to multiple pages at once. A single element in the HTML’s head section can be used to apply the style sheet rules from a CSS file, like so (where style.css is the URL of the CSS file to apply):