Skip to content

Commit

Permalink
mime-types and vec title update
Browse files Browse the repository at this point in the history
  • Loading branch information
aadhavanpl committed Mar 31, 2024
1 parent 33265a5 commit 1c13474
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 25 deletions.
1 change: 1 addition & 0 deletions docs/.vitepress/config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export default defineConfig({
{ text: 'File descriptors', link: '/guides/resources/file-descriptors' },
{ text: 'Linux epoll', link: '/guides/resources/linux-epoll' },
{ text: 'HTTP', link: '/guides/resources/http' },
{ text: 'MIME Types', link: '/guides/resources/mime-types' },
],
},
{
Expand Down
1 change: 1 addition & 0 deletions docs/guides/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- [File Descriptors](/guides/resources/file-descriptors)
- [Linux epoll](/guides/resources/linux-epoll)
- [HTTP](/guides/resources/http)
- [MIME Types](/guides/resources/mime-types)

## References

Expand Down
50 changes: 25 additions & 25 deletions docs/guides/references/vec.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ All vector functions are macro functions. The parameter `v` in each function
should be a pointer to the vec struct which the operation is to be performed
on.
### vec_t(T)
### `vec_t(T)`
Creates a vec struct for containing values of type `T`.
Expand All @@ -366,25 +366,25 @@ Creates a vec struct for containing values of type `T`.
typedef vec_t(FILE*) fp_vec_t;
```

### vec_init(v)
### `vec_init(v)`

Initialises the vector, this must be called before the vector can be used.

### vec_deinit(v)
### `vec_deinit(v)`

Deinitialises the vector, freeing the memory the vector allocated during use;
this should be called when we're finished with a vector.

### vec_push(v, val)
### `vec_push(v, val)`

Pushes a value to the end of the vector. Returns 0 if the operation was
successful, otherwise -1 is returned and the vector remains unchanged.

### vec_pop(v)
### `vec_pop(v)`

Removes and returns the value at the end of the vector.

### vec_splice(v, start, count)
### `vec_splice(v, start, count)`

Removes the number of values specified by `count`, starting at the index
`start`.
Expand All @@ -393,13 +393,13 @@ Removes the number of values specified by `count`, starting at the index
vec_splice(&v, 2, 4); /* Removes the values at indices 2, 3, 4 and 5 */
```
### vec_swapsplice(v, start, count)
### `vec_swapsplice(v, start, count)`
Removes the number of values specified by `count`, starting at the index
`start`; the removed values are replaced with the last `count` values of the
vector. This does not preserve ordering but is O(1).
### vec_insert(v, idx, val)
### `vec_insert(v, idx, val)`
Inserts the value `val` at index `idx` shifting the elements after the index
to make room for the new value.
Expand All @@ -412,73 +412,73 @@ vec_insert(&v, 0, 123);
Returns 0 if the operation was successful, otherwise -1 is returned and the
vector remains unchanged.

### vec_sort(v, fn)
### `vec_sort(v, fn)`

Sorts the values of the vector; `fn` should be a qsort-compatible compare
function.

### vec_swap(v, idx1, idx2)
### `vec_swap(v, idx1, idx2)`

Swaps the values at the indices `idx1` and `idx2` with one another.

### vec_truncate(v, len)
### `vec_truncate(v, len)`

Truncates the vector's length to `len`. If `len` is greater than the vector's
current length then no change is made.

### vec_clear(v)
### `vec_clear(v)`

Clears all values from the vector reducing the vector's length to 0.

### vec_first(v)
### `vec_first(v)`

Returns the first value in the vector. This should not be used on an empty
vector.

### vec_last(v)
### `vec_last(v)`

Returns the last value in the vector. This should not be used on an empty
vector.

### vec_reserve(v, n)
### `vec_reserve(v, n)`

Reserves capacity for at least `n` elements in the given vector; if `n` is
less than the current capacity then `vec_reserve()` does nothing. Returns 0 if
the operation was successful, otherwise -1 is returned and the vector remains
unchanged.

### vec_compact(v)
### `vec_compact(v)`

Reduces the vector's capacity to the smallest size required to store its
current number of values. Returns 0 if the operation is successful, otherwise
-1 is returned and the vector remains unchanged.

### vec_pusharr(v, arr, count)
### `vec_pusharr(v, arr, count)`

Pushes the contents of the array `arr` to the end of the vector. `count` should
be the number of elements in the array.

### vec_extend(v, v2)
### `vec_extend(v, v2)`

Appends the contents of the `v2` vector to the `v` vector.

### vec_find(v, val, idx)
### `vec_find(v, val, idx)`

Finds the first occurrence of the value `val` in the vector. `idx` should be an
int where the value's index will be written; `idx` is set to -1 if `val` could
not be found in the vector.

### vec_remove(v, val)
### `vec_remove(v, val)`

Removes the first occurrence of the value `val` from the vector. If the `val`
is not contained in the vector then `vec_remove()` does nothing.

### vec_reverse(v)
### `vec_reverse(v)`

Reverses the order of the vector's values in place. For example, a vector
containing `4, 5, 6` would contain `6, 5, 4` after reversing.

### vec_foreach(v, var, iter)
### `vec_foreach(v, var, iter)`

Iterates the values of the vector from the first to the last. `var` should be a
variable of the vector's contained type where the value will be stored with
Expand All @@ -493,12 +493,12 @@ vec_foreach(&v, val, i) {
}
```
### vec_foreach_rev(v, var, iter)
### `vec_foreach_rev(v, var, iter)`
Iterates the values of the vector from the last to the first. See
`vec_foreach()`
### vec_foreach_ptr(v, var, iter)
### `vec_foreach_ptr(v, var, iter)`
Iterates the value pointers of the vector from first to last. `var` should be a
variable of the vector's contained type's pointer. See `vec_foreach()`.
Expand All @@ -511,7 +511,7 @@ vec_foreach_ptr(&v, val, i) {
}
```

### vec_foreach_ptr_rev(v, var, iter)
### `vec_foreach_ptr_rev(v, var, iter)`

Iterates the value pointers of the vector from last to first. See
`vec_foreach_ptr()`
Expand Down
85 changes: 85 additions & 0 deletions docs/guides/resources/mime-types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# MIME types

**media type** (also known as a **Multipurpose Internet Mail Extensions or MIME type**) indicates the nature and format of a document, file, or assortment of bytes.

The [Internet Assigned Numbers Authority (IANA)](https://www.iana.org/) is responsible for all official MIME types, and you can find the most up-to-date and complete list at their [Media Types](https://www.iana.org/assignments/media-types/media-types.xhtml) page.

## **What type is my data?**

Bytes are just numbers. So when the client (eg. web browser) receives a stream of bytes from the server, how can it tell if this is Unicode HTML data, binary animated-gif data, or a JPEG vacation photograph? They're all just streams of numbers!

The server has to tell the browser what type this data is. And it does this by specifying the MIME type.

::: details Common MIME types:

- JPEG: `image/jpeg`
- GIF: `image/gif`
- PNG: `image/png`
- JavaScript: `application/javascript`
- JSON: `application/json`
- CSS: `text/css`
- HTML: `text/html`
- Plain TXT: `text/plain`
- Non-descript data: `application/octet-stream`

:::

## **How to determine the type of data served?**

### **Programmatic endpoints**

If you have a programmatic endpoint (e.g. an endpoint that generates the data instead of reading it from disk) then you simply specify the type of the data you're sending back.

For example, if you return data is:

```JSON
{
"animal_type": "goat",
"count": 37
}
```

then you'll use the content type of `application/json`.

### **File serving**

But what if you're reading data from a file and serving it?

If the client requests `http://example.com/foo.png`, we need to reply with a type of `image/png`. The usual way to do this is to simply map between the file extension `.png` and its MIME type `image/png`.

1. Isolate the file extension.
- Examples:
- File is `frotz.jpg`, extension is `.jpg`.
- File is `foo.bar.txt`, extension is `.txt`.
2. Map the extension to its MIME type.
- Example:
- `.txt` maps to `text/plain`
3. If you can't find a mapping for an extension, use `application/octet-stream`.

## **How is the MIME type returned?**

It comes back in the HTTP header in the `Content-Type` field:

```HTTP
Content-Type: text/html
```

Webservers construct their own HTTP headers so the `Content-Type` is included at that time.

## Structure of MIME type

A MIME type most commonly consists of just two parts: a *type* and a *subtype*, separated by a slash (`/`) — with no whitespace between:

```text
type/subtype;parameter=value
```

The **_type_** represents the general category into which the data type falls, such as `video` or `text`.

The **_subtype_** identifies the exact kind of data of the specified type the MIME type represents.

- For example, for the MIME type `text`, the subtype might be `plain``html`, or `calendar`.

An optional **_parameter_** can be added to provide additional details.

- For example, for any MIME type whose main type is `text`, you can add the optional `charset` parameter to specify the character set used for the characters in the data. If no `charset` is specified, the default is [ASCII](https://developer.mozilla.org/en-US/docs/Glossary/ASCII) . To specify a UTF-8 text file, the MIME type `text/plain;charset=UTF-8` is used.

0 comments on commit 1c13474

Please sign in to comment.