Giter Site home page Giter Site logo

simplehttp2server's Introduction

simplehttp2server serves the current directory on an HTTP/2.0 capable server. This server is for development purposes only. simplehttp2server takes a JSON config that allows you to configure headers, redirects and URL rewrites in a lightweight JSON format.

The format is partially compatible with Firebase’s JSON config. Please see disclaimer below.

Installation

Binaries

simplehttp2server is go get-able:

$ go get github.com/GoogleChrome/simplehttp2server

Precompiled binaries can be found in the release section.

Brew

You can also install simplehttp2server using brew if you are on macOS:

$ brew tap GoogleChrome/simplehttp2server https://github.com/GoogleChrome/simplehttp2server
$ brew install simplehttp2server

Docker

If you have Docker set up, you can serve the current directory via simplehttp2server using the following command:

$ docker run -p 5000:5000 -v $PWD:/data surma/simplehttp2server [-config firebase.json]

Usage

Run the simplehttp2server command from the directory you want to serve and go to https://localhost:5000

simplehttp2server [options]
options: 
  -config     string     Config file
  -cors       string     Set allowed origins (default "*")
  -listen     string     Port to listen on (default ":5000")

That browser warning

When you navigate to the server’s address (most likely https://localhost:5000), you will probably get a warning about the connection being insecure similar to the following:

Chrome warning about an insecure certificate

This is normal and correct, since the certificate generated by simplehttp2server is self-signed and doesn’t carry the signature of any common certificate authority (CA). All browsers offer a way to temporarily ignore this error and proceed. This is safe to do.

When using Chrome you can enable the allow-insecure-localhost flag on chrome://flags which disableѕ the certificate warning for localhost. This flag is required if you want to use ServiceWorkers on https://localhost with a self-signed certificate you haven't explicitly "trusted".

Config

simplehttp2server can be configured with the -config flag and a JSON config file. This way you can add custom headers, rewrite rules and redirects. It is partially compatible with Firebase’s JSON config.

All source fields take the Extglob syntax.

Redirects

{
  "redirects": [
    {
      "source": "/shortlinks/a",
      "destination": "https://google.com",
      "type": 301
    }
  ]
}

Rewrites

Rewrites are useful for SPAs, where all paths return index.html and the routing is taking care of in the app itself. Rewrites are only applied when the original target file does not exist.

{
  "rewrites": [
    {
      "source": "/app/**",
      "destination": "/index.html"
    }
  ]
}

Headers

{
  "headers": [
    {
      "source": "/**.html",
      "headers": [
        {
          "key": "Cache-Control",
          "value": "max-age=3600"
        }
      ]
    },
    {
      "source": "/index.html",
      "headers": [
        {
          "key": "Cache-Control",
          "value": "no-cache"
        },
        {
          "key": "Link",
          "value": "</header.jpg>; rel=preload; as=image, </app.js>; rel=preload; as=script"
        }
      ]
    }
  ]
}

For details see the Firebase’s documentation.

Firebase Disclaimer

I haven’t tested if the behavior of simplehttp2server always matches the live server of Firebase, and some options (like trailingSlash and cleanUrls) are completely missing. Please open an issue if you find a discrepancy! The support is not offically endorsed by Firebase (yet 😜), so don’t rely on it!

HTTP/2 PUSH

Any Link headers with rel=preload will be translated to a HTTP/2 PUSH, as is common practice on static hosting platforms and CDNs. See the example above.

License

Apache 2.

simplehttp2server's People

Contributors

crossi36 avatar eduardor2k avatar fippo avatar mike-north avatar ncruces avatar pem-- avatar rongjiecomputer avatar surma avatar tjfogarty avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

simplehttp2server's Issues

Launch the URL.

Hi!

IMHO it should open the URL in the system default browser when it finishes "loading"...

Thanks!

Browsing http://localhost:5000 causes binary file to be downloaded

Description

After starting simplehttp2server, the console shows Listening on :5000.... If we forget that it's serving over https and try to access http://localhost:5000, a binary file gets downloaded.

Steps to Reproduce

  1. CD into directory containing index.html
  2. Run simplehttp2server.
  3. Browse to http://localhost:5000 (instead of https).

Expected Results

  • Server log shows full URL (i.e., Listening on https://localhost:5000)
  • or any of the following:
    • Redirect to https (301)
    • Serve the index.html (unencrypted h2 preferably, or even http/1.1)
    • Display a meaningful/helpful error page
    • Display a meaningful/helpful error on the terminal's server log

Actual Results

  • Binary file is downloaded instead of a page shown in browser
  • Server log emits cryptic (at least for someone unfamiliar with TLS) error:
2016/06/17 22:12:43 http: TLS handshake error from [::1]:54431: tls: first record does not look like a TLS handshake

Environment

x Version
Operating System OS X El Capitan
Go go version go1.6.2 darwin/amd64
Shell zsh
Browser Chrome 51

extglob.go

Hi,

I've redone extglob.go from scratch, lifted a bunch of tests from micromatch.

Only major inaccuracy left (vs. bash), is handling of dotglob (mine/yours matches hidden files by default).

Are you guys interested? If so, I can either contribute, or spin this into its own package.
Haven't done so yet because this is really my first golang effort.

Also planning to implement captures next.

Connection reset when using http

I'm on a windows 10 x64 machine and I'm using sh2sv
When I fire lighthouse it fails when testing http > https redirect with ERR_CONNECTION_RESET
When I visit http://localhost:8080, my console shows multiple errors similar to this:

http: TLS handshake error from [::1]:59634: read tcp [::1]:8080->[::1]:59XXX: use of closed network connection

Is this a known issue? What should I update?

Thank you

Warning Printed to Console when Media Download Finishes

This code causes the messages below to print to the console when it finishes downloading.

disconnecting; error reading frame from client [::1]:50784: read tcp [::1]:5000->[::1]:50784: i/o timeout
disconnecting; error reading frame from client [::1]:50787: read tcp [::1]:5000->[::1]:50787: i/o timeout

Failed installation: `Error: callback() can only be called once.`

I'm getting the following error when running npm install -g simplehttp2server:

/usr/bin/simplehttp2server -> /usr/lib/node_modules/simplehttp2server/cli.js

> [email protected] postinstall /usr/lib/node_modules/simplehttp2server
> node lib/install.js

/usr/lib/node_modules/simplehttp2server/node_modules/onetime/index.js:15
				throw new Error(fnName + ' can only be called once.');
				^

Error: callback() can only be called once.
    at onetime (/usr/lib/node_modules/simplehttp2server/node_modules/onetime/index.js:15:11)
    at /usr/lib/node_modules/simplehttp2server/node_modules/download/index.js:156:5
    at ConcatStream.<anonymous> (/usr/lib/node_modules/simplehttp2server/node_modules/concat-stream/index.js:36:43)
    at emitNone (events.js:110:20)
    at ConcatStream.emit (events.js:207:7)
    at finishMaybe (/usr/lib/node_modules/simplehttp2server/node_modules/readable-stream/lib/_stream_writable.js:607:14)
    at endWritable (/usr/lib/node_modules/simplehttp2server/node_modules/readable-stream/lib/_stream_writable.js:615:3)
    at ConcatStream.Writable.end (/usr/lib/node_modules/simplehttp2server/node_modules/readable-stream/lib/_stream_writable.js:571:41)
    at DuplexWrapper.onend (/usr/lib/node_modules/simplehttp2server/node_modules/readable-stream/lib/_stream_readable.js:570:10)
    at Object.onceWrapper (events.js:314:30)

Tested on the latest versions of node (8.1.3) and npm (5.0.4).
I'm running Arch Linux.

HTTP -> HTTPS redirect not firing

Sup dude,

When I hit localhost:5000 on http I don't get a redirect to https. Looks like it knows about it:

http: TLS handshake error from [::1]:61421: tls: first record does not look like a TLS handshake

Should I expect it to redirect?

Patch HTTP method missing in Access-Control-Allowed-Methods header property

The Problem

On line 83 of simplehttp2server.go it seems PATCH is omitted in a context that it shouldn't.

Side-effect

Unless it's simply not supported as I think it is, PATCH being omitted can unexpectedly break the Principle of Least Astonishment when partially updating representation of resources on the server with a set of operations or the resource sent back itself.

The Suggestion Action

If I'm correct, Line 83 can be changed to the following:

w.Header().Set("Access-Control-Allow-Methods", "GET, OPTION, HEAD, PUT, PATCH, POST, DELETE")

No usage section in the readme

It's quite obvious for most developers how to use simplehttp2server, just run the simplehttp2server command after installing it in the directory you want to serve. However when first reading the docs i was instinctively looking (skimming 😄) for a "usage section" and thinking "how do i run this?"

Being a developer i just ran the command in terminal and of course that worked. However especially for developers that are quite new to the terminal i would reckon they would be quite puzzled since there's not really a usage section in the readme. There's only a section about the config flag that implies you can run simplehttp2server -config.

tl;dr: Maybe add a section between Docker and Config (example below) that explains usage of simplehttp2server more explicitly. Thoughts on this?

Usage

Run the simplehttp2server command from the directory you want to serve to https://localhost:5000 (default)

simplehttp2server [options]
options: 
  -config     string     Config file
  -cors       string     Set allowed origins (default "*")
  -listen     string     Port to listen on (default ":5000")

Does not work with latest Chrome

Using latest Google Chrome v. 85.0.4183.83 on MacOS Catalina 10.15.6 (19G2021).

Using latest precompiled binary simplehttp2server_darwin_amd64 from 2017.

No config, all default settings.

Redirection http => https does not work: "This site can’t be reached"

https://localhost:5000 does not work: "NET::ERR_CERT_INVALID" and Advanced does not list a way to proceed due to "You cannot visit localhost at the moment because the website sent scrambled credentials that Google Chrome cannot process. "

Log excerpt:
$ ./simplehttp2server_darwin_amd64 2020/08/26 17:06:54 Generating certificate... 2020/08/26 17:06:54 written cert.pem 2020/08/26 17:06:54 written key.pem 2020/08/26 17:06:54 Listening on https://localhost:5000... 2020/08/26 17:06:59 http: TLS handshake error from [::1]:63035: remote error: tls: unknown certificate

Not able to open the URL on latest version of Chrome?

Hi!

Not able to open https://localhost:5000/ on chrome Version 53.0.2767.4 dev (64-bit) and Version 51.0.2704.103 (64-bit) on (Linux Mint)

Normaly I would go to the advanced option and would say something like "add exception", but it doesn't have that option... is this related to the tool or Chrome?
image

I've also tried with https://127.0.0.1:5000/ and the same happens...

It opened on Firefox.... but seriously, who uses Firefox!? 😄 (flame war started!)

Thanks!

script has an unsupported MIME type ('text/plain')

Trying to use simplehttp2server as a development server to conduct Lighthouse testing. It is not working because the server is setting content-type for javascript files to 'text/plain' rather than ​'application/javascript'. As a result, the service worker fails to load. How could this issue be resolved?

Push manifest

Why not use github.com/google/http2preload for that?

Certificate error (version 2.3.1) - How can it be fixed?

I tried running the server(version 2.3.1) but got this error in my console:

Uncaught (in promise) DOMException: Failed to register a ServiceWorker: An SSL certificate error occurred when fetching the script.

This obviously didn't allow SW to be registered in my app.

How do I fix this?

Using Node/PHP/Python?

Ow, loving it, simple and works.. Is it possible to tell the server run the code in node/php/python?
This repository motivating me to learn Go 💯

Installation error

OS: MacOS Sierra

When installing I always get an exception (found at preactjs/preact-cli#26):

$ npm i simplehttp2server
> [email protected] postinstall /Users/marvinhagemeister/dev/http2server/node_modules/simplehttp2server
> node lib/install.js

  ⚠ The `/Users/marvinhagemeister/dev/http2server/node_modules/simplehttp2server/vendor/simplehttp2server` binary doesn't seem to work correctly
  ⚠ simplehttp2server pre-build test failed
  ℹ compiling from source
  ✖ Error: mkdir -p /Users/marvinhagemeister/dev/http2server/node_modules/simplehttp2server/vendor && sh crosscompile.sh && mv ./0f3bf125-3c46-428e-94d8-d7546e50273f_darwin_amd64 /Users/marvinhagemeister/dev/http2server/node_modules/simplehttp2server/vendor/simplehttp2server
Command failed: sh crosscompile.sh && mv ./0f3bf125-3c46-428e-94d8-d7546e50273f_darwin_amd64 /Users/marvinhagemeister/dev/http2server/node_modules/simplehttp2server/vendor/simplehttp2server
simplehttp2server.go:24:2: cannot find package "github.com/NYTimes/gziphandler" in any of:
	/usr/local/Cellar/go/1.8.1/libexec/src/github.com/NYTimes/gziphandler (from $GOROOT)
	/Users/marvinhagemeister/go/src/github.com/NYTimes/gziphandler (from $GOPATH)
simplehttp2server.go:24:2: cannot find package "github.com/NYTimes/gziphandler" in any of:
	/usr/local/Cellar/go/1.8.1/libexec/src/github.com/NYTimes/gziphandler (from $GOROOT)
	/Users/marvinhagemeister/go/src/github.com/NYTimes/gziphandler (from $GOPATH)
simplehttp2server.go:24:2: cannot find package "github.com/NYTimes/gziphandler" in any of:
	/usr/local/Cellar/go/1.8.1/libexec/src/github.com/NYTimes/gziphandler (from $GOROOT)
	/Users/marvinhagemeister/go/src/github.com/NYTimes/gziphandler (from $GOPATH)
simplehttp2server.go:24:2: cannot find package "github.com/NYTimes/gziphandler" in any of:
	/usr/local/Cellar/go/1.8.1/libexec/src/github.com/NYTimes/gziphandler (from $GOROOT)
	/Users/marvinhagemeister/go/src/github.com/NYTimes/gziphandler (from $GOPATH)
simplehttp2server.go:24:2: cannot find package "github.com/NYTimes/gziphandler" in any of:
	/usr/local/Cellar/go/1.8.1/libexec/src/github.com/NYTimes/gziphandler (from $GOROOT)
	/Users/marvinhagemeister/go/src/github.com/NYTimes/gziphandler (from $GOPATH)

    at ChildProcess.exithandler (child_process.js:205:12)
    at emitTwo (events.js:106:13)
    at ChildProcess.emit (events.js:194:7)
    at maybeClose (internal/child_process.js:899:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5)
[email protected] /Users/marvinhagemeister/dev/http2server

Warning when installing on OS X 10.12

Warning: You are using OS X 10.12.
We do not provide support for this pre-release version.
You may encounter build failures or other breakages.

If I find anything broken or failing, I'll add it to this ticket.

Problem with Windows binary

I’m on Windows 10 (build 14342). When I run the “simplehttp2server_windows_amd64” binary in the command line, I get the “How do you want to open this file?” dialog. Is this a known issue?

(I’ve downloaded the binary, renamed it to “simplehttp2server”, and moved it into one of my directories that’s in my PATH.)

An SSL certificate error occurred when fetching the script

There seem to be issues with loading service worker scripts using simpleHtt2server
How can one get past this?

An SSL certificate error occurred when fetching the script.
:5000/service-worker.js Failed to load resource: net::ERR_INSECURE_RESPONSE
(index):30 Registration failed with SecurityError: Failed to register a ServiceWorker: An SSL certificate error occurred when fetching the 

Failure running simplehttp2server_darwin_amd64, v. 2.1.0

While I can run simplehttp2server_darwin_amd64 v. 2.1.0, when I try to access https://localhost:5000, it fails with the trace below. This does not happen if I switch back to v. 2.0.2. I am running MacOS 10.10.5.

$ ~/downloads/simplehttp2server_darwin_amd64
2016/06/29 10:47:01 Listening on https://localhost:5000...
2016/06/29 10:47:09 Request for / (Accept-Encoding: gzip, deflate, sdch, br)
panic: invalid argument to Intn

goroutine 21 [running]:
panic(0x2f9be0, 0xc820158470)
  /Users/surma/.homebrew/Cellar/go/1.6.2/libexec/src/runtime/panic.go:481 +0x3e6
math/rand.(*Rand).Intn(0xc8200642b0, 0x0, 0x38)
  /Users/surma/.homebrew/Cellar/go/1.6.2/libexec/src/math/rand/rand.go:98 +0x79
math/rand.Intn(0x0, 0x24)
  /Users/surma/.homebrew/Cellar/go/1.6.2/libexec/src/math/rand/rand.go:213 +0x2d
main.main.func2(0x1308910, 0xc820088050, 0xc82013c1c0)
  /Users/surma/src/github.com/GoogleChrome/simplehttp2server/simplehttp2server.go:92 +0x2aa
net/http.HandlerFunc.ServeHTTP(0xc8201026a0, 0x1308910, 0xc820088050, 0xc82013c1c0)
  /Users/surma/.homebrew/Cellar/go/1.6.2/libexec/src/net/http/server.go:1618 +0x3a
net/http.serverHandler.ServeHTTP(0xc820094080, 0x1308910, 0xc820088050, 0xc82013c1c0)
  /Users/surma/.homebrew/Cellar/go/1.6.2/libexec/src/net/http/server.go:2081 +0x19e
net/http.initNPNRequest.ServeHTTP(0xc82013a300, 0xc820094080, 0x1308910, 0xc820088050, 0xc82013c1c0)
  /Users/surma/.homebrew/Cellar/go/1.6.2/libexec/src/net/http/server.go:2489 +0x221
net/http.(*initNPNRequest).ServeHTTP(0xc820158340, 0x1308910, 0xc820088050, 0xc82013c1c0)
  <autogenerated>:253 +0xb6
github.com/GoogleChrome/simplehttp2server/http2.(*serverConn).runHandler(0xc82029e000, 0xc820088050, 0xc82013c1c0)
  /Users/surma/src/github.com/GoogleChrome/simplehttp2server/http2/server.go:1604 +0xa4
created by github.com/GoogleChrome/simplehttp2server/http2.(*serverConn).processHeaderBlockFragment
  /Users/surma/src/github.com/GoogleChrome/simplehttp2server/http2/server.go:1466 +0x387

"hosting" property not handled

simplehttp2server doesn't seem to correctly handle firebase.json files in cases where the public key is a child of the hosting key.

For example, this firebase.json from the documentation is not parsed correctly:

{
  "hosting": {
    "public": "app",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ]
  }
}

(The public key is ignored, and simplehttp2server serves from the "root".)

To make it work as expected, the public key needs to be moved to the root:

{
  "public": "app",
  "hosting": {
    "public": "app",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ]
  }
}

Always encodes response with gzip

$ curl -k https://localhost:5000/ | gunzip
<pre>
<a href=".git/">.git/</a>
...
2016/07/26 21:52:57 Request for / (Accept-Encoding: )
2016/07/26 21:52:57 Could not load push manifest "push.json": open push.json: no such file or directory

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.