Skip to content

Commit

Permalink
feat: Introduce Relic web sever (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
klkucaj authored Dec 13, 2024
1 parent ddc0321 commit 7656267
Show file tree
Hide file tree
Showing 166 changed files with 25,392 additions and 52 deletions.
1 change: 1 addition & 0 deletions .pubignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test/ssl_certs.dart
49 changes: 25 additions & 24 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
BSD 3-Clause License

Copyright (c) 2024, Serverpod
Copyright 2014, the Dart project authors.
Copyright 2015, the Dart project authors.
Copyright 2024, the Serverpod project authors.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
modification, are permitted provided that the following conditions are
met:

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Google LLC nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Relic

Relic is a basic HTTP server based on Shelf. Unlike Shelf, Relic is strictly typed, and it has some other performance improvements. This release is still experimental.

Relic is primarily created for [Serverpod](https://serverpod.dev).
37 changes: 32 additions & 5 deletions lib/relic.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
/// Support for doing something awesome.
///
/// More dartdocs go here.
library;

export 'src/relic_base.dart';
/// Body related exports
export 'src/body/body.dart' show Body;
export 'src/body/types/body_type.dart' show BodyType;
export 'src/body/types/mime_type.dart' show MimeType;

// TODO: Export any libraries intended for clients of this package.
/// Handler related exports
export 'src/handler/cascade.dart' show Cascade;
export 'src/handler/handler.dart' show Handler;
export 'src/handler/pipeline.dart' show Pipeline;

// Headers related exports
export 'src/headers/headers.dart' show Headers;
export 'src/headers/typed/typed_headers.dart';
export 'src/headers/custom/custom_headers.dart' show CustomHeaders;

/// Hijack related exports
export 'src/hijack/exception/hijack_exception.dart' show HijackException;

/// Message related exports
export 'src/message/request.dart' show Request;
export 'src/message/response.dart' show Response;

/// Middleware related exports
export 'src/middleware/middleware_logger.dart' show logRequests;
export 'src/middleware/middleware.dart' show Middleware, createMiddleware;
export 'src/middleware/middleware_extensions.dart' show MiddlewareExtensions;

/// Relic server related exports
export 'src/relic_server.dart' show RelicServer;
export 'src/relic_server_serve.dart' show serve;

/// Static handler export
export 'src/static/static_handler.dart';
139 changes: 139 additions & 0 deletions lib/src/body/body.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';

import 'package:relic/src/body/types/body_type.dart';
import 'package:relic/src/body/types/mime_type.dart';

/// The body of a request or response.
///
/// This tracks whether the body has been read. It's separate from [Message]
/// because the message may be changed with [Message.copyWith], but each instance
/// should share a notion of whether the body was read.
class Body {
/// The contents of the message body.
///
/// This will be `null` after [read] is called.
Stream<Uint8List>? _stream;

/// The length of the stream returned by [read], or `null` if that can't be
/// determined efficiently.
final int? contentLength;

/// The content type of the body.
///
/// This will be `null` if the body is empty.
///
/// This is a convenience property that combines [mimeType] and [encoding].
/// Example:
/// ```dart
/// var body = Body.fromString('hello', mimeType: MimeType.plainText);
/// print(body.contentType); // ContentType(text/plain; charset=utf-8)
/// ```
final BodyType? contentType;

Body._(
this._stream,
this.contentLength, {
Encoding? encoding,
MimeType? mimeType,
}) : contentType = mimeType == null
? null
: BodyType(mimeType: mimeType, encoding: encoding);

/// Creates an empty body.
factory Body.empty({
Encoding encoding = utf8,
MimeType mimeType = MimeType.plainText,
}) =>
Body._(
Stream.empty(),
0,
encoding: encoding,
mimeType: mimeType,
);

/// Creates a body from a [HttpRequest].
factory Body.fromHttpRequest(HttpRequest request) {
var contentType = request.headers.contentType;
return Body._(
request,
request.contentLength <= 0 ? null : request.contentLength,
encoding: Encoding.getByName(contentType?.charset),
mimeType: contentType?.toMimeType,
);
}

/// Creates a body from a string.
factory Body.fromString(
String body, {
Encoding encoding = utf8,
MimeType mimeType = MimeType.plainText,
}) {
Uint8List encoded = Uint8List.fromList(encoding.encode(body));
return Body._(
Stream.value(encoded),
encoded.length,
encoding: encoding,
mimeType: mimeType,
);
}

/// Creates a body from a [Stream] of [Uint8List].
factory Body.fromDataStream(
Stream<Uint8List> body, {
Encoding? encoding = utf8,
MimeType? mimeType = MimeType.plainText,
int? contentLength,
}) {
return Body._(
body,
contentLength,
encoding: encoding,
mimeType: mimeType,
);
}

/// Creates a body from a [Uint8List].
factory Body.fromData(
Uint8List body, {
Encoding? encoding,
MimeType mimeType = MimeType.binary,
}) {
return Body._(
Stream.value(body),
body.length,
encoding: encoding,
mimeType: mimeType,
);
}

/// Returns a [Stream] representing the body.
///
/// Can only be called once.
Stream<Uint8List> read() {
var stream = _stream;
if (stream == null) {
throw StateError(
"The 'read' method can only be called once on a "
'Request/Response object.',
);
}
_stream = null;
return stream;
}

/// Returns the content type of the body as a [ContentType].
///
/// This is a convenience method that combines [mimeType] and [encoding].
ContentType? getContentType() {
var mContentType = contentType;
if (mContentType == null) return null;
return ContentType(
mContentType.mimeType.primaryType,
mContentType.mimeType.subType,
charset: mContentType.encoding?.name,
);
}
}
86 changes: 86 additions & 0 deletions lib/src/body/types/body_type.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import 'dart:convert';

import 'package:relic/src/body/types/mime_type.dart';

/// A body type.
class BodyType {
/// A body type for plain text.
static const plainText = BodyType(
mimeType: MimeType.plainText,
encoding: utf8,
);

/// A body type for HTML.
static const html = BodyType(
mimeType: MimeType.html,
encoding: utf8,
);

/// A body type for CSS.
static const css = BodyType(
mimeType: MimeType.css,
encoding: utf8,
);

/// A body type for CSV.
static const csv = BodyType(
mimeType: MimeType.csv,
encoding: utf8,
);

/// A body type for JavaScript.
static const javaScript = BodyType(
mimeType: MimeType.javaScript,
encoding: utf8,
);

/// A body type for JSON.
static const json = BodyType(
mimeType: MimeType.json,
encoding: utf8,
);

/// A body type for XML.
static const xml = BodyType(
mimeType: MimeType.xml,
encoding: utf8,
);

/// A body type for binary data.
static const binary = BodyType(
mimeType: MimeType.binary,
);

/// A body type for PDF.
static const pdf = BodyType(
mimeType: MimeType.pdf,
);

/// A body type for RTF.
static const rtf = BodyType(
mimeType: MimeType.rtf,
);

/// The mime type of the body.
final MimeType mimeType;

/// The encoding of the body.
final Encoding? encoding;

const BodyType({
required this.mimeType,
this.encoding,
});

/// Returns the value to use for the Content-Type header.
String toHeaderValue() {
if (encoding != null) {
return '${mimeType.toHeaderValue()}; charset=${encoding!.name}';
} else {
return mimeType.toHeaderValue();
}
}

@override
String toString() => 'BodyType(mimeType: $mimeType, encoding: $encoding)';
}
Loading

0 comments on commit 7656267

Please sign in to comment.