Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Builds, tests and packs on every push and pull request. A tag named
# v<version> (e.g. v2.0.1) also publishes that version to nuget.org through
# Trusted Publishing: no API key is stored, the publish job trades GitHub's
# OIDC token for a short-lived nuget.org key.
#
# The nuget.org Trusted Publishing policy matches on repository owner,
# repository name, this file's name (build.yml) and, if it names one, the
# `production` environment: renaming this file breaks publishing.

name: Build

on:
push:
branches: [main, gen3]
tags: ['v*']
pull_request:

permissions:
contents: read

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7

- uses: actions/setup-dotnet@v6
with:
dotnet-version: 10.0.x

- name: Test
run: dotnet test Cosmos.Network.Http.slnx -c Release

# On a release tag the tag names the version; otherwise the .csproj does.
- name: Pack
run: |
args=()
if [[ "$GITHUB_REF" == refs/tags/v* ]]; then
args+=("-p:Version=${GITHUB_REF_NAME#v}")
fi
dotnet pack src/Cosmos.Network.Http/Cosmos.Network.Http.csproj -c Release -o artifacts "${args[@]}"

- uses: actions/upload-artifact@v7
with:
name: nupkg
path: artifacts/*.nupkg

publish:
needs: build
if: startsWith(github.ref, 'refs/tags/v')
runs-on: ubuntu-latest
environment: production
permissions:
id-token: write # lets NuGet/login request GitHub's OIDC token
steps:
- uses: actions/download-artifact@v8
with:
name: nupkg
path: artifacts

- name: NuGet login
id: login
uses: NuGet/login@v1
with:
# The user who CREATED the policy, not the CosmosOS profile that owns
# it. nuget.org looks the policy up by creator and answers a wrong
# guess with "No matching trust policy owned by user 'CosmosOS' was
# found" (HTTP 401). Public, so not a secret.
user: valentinbreiz

- name: Push to nuget.org
run: dotnet nuget push "artifacts/*.nupkg" --api-key "${{ steps.login.outputs.NUGET_API_KEY }}" --source https://api.nuget.org/v3/index.json
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
bin
obj
.vs
.vs
artifacts
8 changes: 8 additions & 0 deletions Cosmos.Network.Http.slnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Solution>
<Folder Name="/src/">
<Project Path="src/Cosmos.Network.Http/Cosmos.Network.Http.csproj" />
</Folder>
<Folder Name="/tests/">
<Project Path="tests/Cosmos.Network.Http.Tests/Cosmos.Network.Http.Tests.csproj" />
</Folder>
</Solution>
42 changes: 0 additions & 42 deletions CosmosHttp.csproj

This file was deleted.

94 changes: 57 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,70 +1,90 @@
<h1 align="center">CosmosHTTP Client [WIP]</h1>
<h1 align="center">CosmosHTTP Client 🚀</h1>
<p>
<a href="https://www.nuget.org/packages/CosmosHttp/" target="_blank">
<img alt="Version" src="https://img.shields.io/nuget/v/CosmosHttp.svg" />
<a href="https://www.nuget.org/packages/Cosmos.Network.Http/" target="_blank">
<img alt="Version" src="https://img.shields.io/nuget/v/Cosmos.Network.Http.svg" />
</a>
<a href="https://github.com/CosmosOS/CosmosHttp/blob/main/LICENSE.txt" target="_blank">
<a href="https://github.com/CosmosOS/Cosmos.Network.Http/blob/main/LICENSE.txt" target="_blank">
<img alt="License: BSD Clause 3 License" src="https://img.shields.io/badge/license-BSD License-yellow.svg" />
</a>
</p>

> CosmosHTTP is a HTTP client made in C# for the Cosmos operating system construction kit. GET and PUT are currently supported.

### Todo
See [this issue](https://github.com/CosmosOS/CosmosHttp/issues/1) for todo list.
> CosmosHTTP is an HTTP/1.1 client made in C# for the Cosmos operating system construction kit.

## Usage

### Installation

Install the Nuget Package from [Nuget](https://www.nuget.org/packages/CosmosHttp/):
Add the package to your kernel .csproj:

```PM
Install-Package CosmosHttp -Version 1.0.4
```xml
<ItemGroup>
<PackageReference Include="Cosmos.Network.Http" Version="2.0.0" />
</ItemGroup>
```

```PM
dotnet add PROJECT package CosmosHttp --version 1.0.4
```
The kernel needs networking (`CosmosEnableNetwork`, on by default), an IP configuration (DHCP or static) and, for host names, a DNS server. `Send()` runs the request on the calling thread and returns the response once it has arrived whole:

```csharp
using System;
using System.IO;
using Cosmos.Network.Http;

Or add these lines to your Cosmos kernel .csproj:
HttpResponse response = new HttpRequest("http://httpforever.com/").Send();

Console.WriteLine($"{response.StatusCode} {response.ReasonPhrase}, {response.Content.Length} bytes");
File.WriteAllBytes("/0/index.html", response.Content);
```
<ItemGroup>
<PackageReference Include="CosmosHttp" Version="1.0.4" />
</ItemGroup>

`Send()` returns error statuses too; `EnsureSuccessStatusCode()` turns them into an `HttpException`, and `GetString()` decodes the body with the charset of its Content-Type:

```csharp
string json = new HttpRequest("http://example.com/data.json").Send().EnsureSuccessStatusCode().GetString();
```

### Examples
A request can set its method, body, headers, timeout and how many redirects it follows:

```csharp
HttpResponse response = new HttpRequest("http://example.com/api")
{
Method = "POST",
Body = Encoding.UTF8.GetBytes("{\"name\":\"cosmos\"}"),
Headers = { ["Content-Type"] = "application/json" },
Timeout = 10_000, // how long the server may stay silent, in milliseconds (15 s by default)
MaxRedirects = 0, // return redirects instead of following them (5 by default)
// Optional: one line per response and redirect.
Log = message => Cosmos.Kernel.System.Diagnostics.Log.WriteString(message + "\n"),
}.Send();
```

```CS
using CosmosHttp.Client;
The Host header comes from the URL. Setting it in `Headers` reaches a virtual host by IP address:

HttpRequest request = new();
request.IP = "34.223.124.45";
request.Domain = "neverssl.com"; //very useful for subdomains on same IP
request.Path = "/";
request.Method = "GET";
request.Send();
Console.WriteLine(request.Response.Content); // or to get bytes Encoding.ASCII.getString(request.Response.GetStream())
```csharp
new HttpRequest("http://34.223.124.45/") { Headers = { ["Host"] = "neverssl.com" } }.Send();
```

Here is a basic wget command implementation using CosmosHttp: [github.com/aura-systems/Aura-Operating-System](https://github.com/aura-systems/Aura-Operating-System/blob/master/SRC/Aura_OS/System/Interpreter/Commands/Network/Wget.cs#L63).
### Limits

- `http://` only: there is no TLS, so `https://` URLs, and redirects to them, throw.
- Each request opens a connection of its own, and the server closes it once it has answered.
- Responses come without content coding (`Accept-Encoding: identity`): a Cosmos kernel has no gzip to undo.
- The whole body is held in memory.

### Threads

`Send()` never waits in `Thread.Sleep`: it waits in `Socket.Poll`, which returns at once on a Cosmos kernel. So it runs on the kernel's main loop, which must never block, as well as on a thread of its own.

## Authors

👤 **[@valentinbreiz](https://github.com/valentinbreiz)**

👤 **[@2881099](https://github.com/2881099)**
👤 **[@2881099](https://github.com/2881099)** (the first version was inspired by [TcpClientHttpRequest](https://github.com/2881099/TcpClientHttpRequest))

## 🤝 Contributing

Contributions, issues and feature requests are welcome! Feel free to check [issues page](https://github.com/CosmosOS/CosmosHttp/issues).
Contributions, issues and feature requests are welcome!

## Show your support

Give a ⭐️ if this project helped you!
Feel free to check [issues page](https://github.com/CosmosOS/Cosmos.Network.Http/issues).

## 📝 License

Copyright © 2023 [CosmosOS](https://github.com/CosmosOS). This project is [BSD Clause 3](https://github.com/CosmosOS/CosmosHttp/blob/main/LICENSE.txt) licensed.
Copyright © 2023-2026 [CosmosOS](https://github.com/CosmosOS).

This project is [BSD Clause 3](https://github.com/CosmosOS/Cosmos.Network.Http/blob/main/LICENSE.txt) licensed.
Binary file modified resources/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading