hlfw.ca

plumb

Download patch

ref: cafc956f735ed7620fa3e00141eaa6f4bb4e76ea
parent: 3a49d4e373f8e2887bea84ee3d2fca14bbc6f7fa
author: halfwit <michaelmisch1985@gmail.com>
date: Thu Jan 24 17:33:24 PST 2019

Add content-type fallback, text fallback, add readme

--- a/README.md
+++ b/README.md
@@ -1,0 +1,20 @@
+# Plumb
+
+## Overview
+
+plumb is a drop-in replacement for plan9's plumb command, that understands mime-types of local files and URLs. 
+
+## Rules
+
+plumb will write plumb messages as the usual `type is text` for non-files, and non-URLs. When the content is one of those, however, it will set the type to that mime.
+
+```
+
+type is image/png
+type is text/html
+type is text/plain
+type is application/pdf
+
+```
+
+If the remote mimetype is `application/octet-stream`, which is a fallback when it cannot infer the mimetype this client will attempt to find a content-type field in any remote URL, finally setting the type to `text`.
--- a/plumb.go
+++ b/plumb.go
@@ -98,7 +98,14 @@
 	if err != nil {
 		return "", err
 	}
-	return http.DetectContentType(buf[:n]), nil
+	mediaType := http.DetectContentType(buf[:n])
+	if (mediaType == "application/octet-stream" || mediaType == "" ) {
+		mediaType := r.Header.Get("Content-type")
+		if mediaType == "" {
+			return "text", nil
+		}
+	}
+	return mediaType, nil
 }
 
 func contentTypeFile(arg string) (string, error) {
@@ -112,7 +119,11 @@
 	if err != nil {
 		return "", err
 	}
-	return http.DetectContentType(buf[:n]), nil
+	mediaType := http.DetectContentType(buf[:n])
+	if mediaType == "application/octet-stream" {
+		return "text", nil
+	}
+	return mediaType, nil
 }
 
 func content(arg string) (string, error) {