summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authordakkar <dakkar@thenautilus.net>2017-12-16 20:14:08 +0000
committerdakkar <dakkar@thenautilus.net>2017-12-16 20:26:14 +0000
commitdb1f46d1af765dfa5cca3d23a9e7246d4966ccd8 (patch)
tree408106d9678e307e411f53c1039a943b9f662cc6 /lib
parentfix authorisation to return Subsonic response (diff)
downloadUltramarine-db1f46d1af765dfa5cca3d23a9e7246d4966ccd8.tar.gz
Ultramarine-db1f46d1af765dfa5cca3d23a9e7246d4966ccd8.tar.bz2
Ultramarine-db1f46d1af765dfa5cca3d23a9e7246d4966ccd8.zip
rough server w/ serialisation
Diffstat (limited to 'lib')
-rw-r--r--lib/Ultramarine/Middleware/SetContentType.pm626
-rw-r--r--lib/Ultramarine/Serialiser/XML.pm630
2 files changed, 56 insertions, 0 deletions
diff --git a/lib/Ultramarine/Middleware/SetContentType.pm6 b/lib/Ultramarine/Middleware/SetContentType.pm6
new file mode 100644
index 0000000..a3fab94
--- /dev/null
+++ b/lib/Ultramarine/Middleware/SetContentType.pm6
@@ -0,0 +1,26 @@
+use v6.d.PREVIEW;
+use Cro::HTTP::Middleware;
+use Cro::HTTP::BodySerializerSelector;
+use Ultramarine::Serialiser::XML;
+
+my %types = (
+ xml => 'application/xml',
+ json => 'application/json',
+ jsonp => 'application/javascript',
+);
+
+class Ultramarine::Middleware::SetContentType
+ does Cro::HTTP::Middleware::Response {
+ method process(Supply:D $response-stream) {
+ supply whenever $response-stream -> $response {
+ with $response.request {
+ my $format = .query-value('f')[0] // 'xml';
+ with %types{$format} {
+ $response.append-header('Content-type',$_);
+ }
+ }
+
+ emit $response;
+ }
+ }
+}
diff --git a/lib/Ultramarine/Serialiser/XML.pm6 b/lib/Ultramarine/Serialiser/XML.pm6
new file mode 100644
index 0000000..7bb14b8
--- /dev/null
+++ b/lib/Ultramarine/Serialiser/XML.pm6
@@ -0,0 +1,30 @@
+use v6.d.PREVIEW;
+use Cro::HTTP::BodySerializer;
+use XML::Writer;
+
+class Ultramarine::Serialiser::XML
+ does Cro::HTTP::BodySerializer {
+
+ method is-applicable(Cro::HTTP::Message $message, $body --> Bool) {
+ with $message.content-type {
+ (.type eq 'application' && .subtype eq 'xml' || .suffix eq 'xml') &&
+ ($body ~~ Map || $body ~~ List)
+ }
+ else {
+ False
+ }
+ }
+
+ method serialize(Cro::HTTP::Message $message, $body --> Supply) {
+ my $xml = XML::Writer.serialize(
+ subsonic-response => [
+ :xmlns<http://subsonic.org/restapi>,
+ :version<1.13.0>,
+ |$body,
+ ],
+ ).encode('utf-8');
+ self!set-content-length($message, $xml.bytes);
+ supply { emit $xml }
+ }
+
+}