3 Star 4 Fork 1

MJ PC Lab / extra-http-file-server

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
contribute
Sync branch
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README
MIT

Extra HTTP File Server

Extra HTTP File Server is based on Go HTTP File Server, with extra features. It provides frequently used features for a simple static website.

Extra HTTP File Server pages

Different to Go HTTP File Server

Code base

Based on Go HTTP File Server's main branch, dropped support for legacy Go version. This means it is impossible to use legacy Go version to compile binaries for legacy systems, e.g. Windows XP.

Changed behavior

For PKI validation URL /.well-known/, will skip redirecting from http: to https: even --to-https is specified.

New options

--ip-allow <IP>|<network/prefix> ...
--ip-allow-file <file> ...
    Only allow client access from specific IP or network.
    Unmatched client IP will be denied.

--ip-deny <IP>|<network/prefix> ...
--ip-deny-file <file> ...
    Only denly client access from specific IP or network.
    Unmatched client IP will be allowed to access.

--rewrite-host <separator><match><separator><replace>
    Transform a request host+URL (in the form of "host[:port]/request/path?param=value")
    into another URL if it is matched by regular expression `match`.

    The rewrite target is specified by `replace`.
    Use `$0` to represent the whole match in `match`.
    use `$1` - `$9` to represent sub matches in `match`.
--rewrite-host-post <sep><match><sep><replace>
    Similar to --rewrite-host, but executes after redirects has no match.
--rewrite-host-end <sep><match><sep><replace>
    Similar to --rewrite-host-post, but skip rest process if matched.

--rewrite <separator><match><separator><replace>
    Transform a request URL (in the form of "/request/path?param=value")
    into another one if it is matched by regular expression `match`.

    The rewrite target is specified by `replace`.
    Use `$0` to represent the whole match in `match`.
    use `$1` - `$9` to represent sub matches in `match`.
--rewrite-post <sep><match><sep><replace>
    Similar to --rewrite, but executes after redirects has no match.
--rewrite-end <sep><match><sep><replace>
    Similar to --rewrite-post, but skip rest process if matched.

--redirect <separator><match><separator><replace>[<separator><status-code>]
    Perform an HTTP redirect when request URL (in the form of "/request/path?param=value")
    is matched by regular expression `match`.

    The redirect target is specified by `replace`.
    Use `$0` to represent the whole match in `match`.
    use `$1` - `$9` to represent sub matches in `match`.

    Optional `status_code` specifies HTTP redirect code. defaults to 301.

--proxy <separator><match><separator><replace>
    Proxy a request URL (in the form of "/request/path?param=value")
    to target if it is matched by regular expression `match`.

    The proxy target is specified by `replace`.
    Use `$0` to represent the whole match in `match`.
    use `$1` - `$9` to represent sub matches in `match`.

--return <separator><match><separator><status-code>
    When request URL (in the form of "/request/path?param=value")
    is matched by `match`, return the status code `status-code`
    immediately and stop processing.
--to-status <separator><match><separator><status-code>
    Similar to --return, but process after ghfs internal process finished.

--status-page <separator><status-code><separator><fs-path>
    When response status is `status-code`, respond with the file content from `fs-path`.

--gzip-static
    When requesting for FILE, if client supports gzip decoding, try looking for and
    outputing FILE.gz as gzip compressed content.

--header-add <separator><match><separator><name><separator><value>
--header-set <separator><match><separator><name><separator><value>
    Add or set response header if URL(in the form of "/request/path?param=value")
    matches `match`.

Processing order

  • if client IP not match --ip-allow or --ip-allow-file, return status 403, and stop processing
    • --status-page executed if status code matched, and stop processing.
  • if client IP match --ip-deny or --ip-deny-file, return status 403, and stop processing
    • --status-page executed if status code matched, and stop processing.
  • --rewrite-host and --rewrite executed to transform the URL if matched.
  • --redirect executed if URL matched, and stop processing.
  • --rewrite-host-post and --rewrite-post executed to transform the URL if matched.
  • --rewrite-host-end and --rewrite-end executed to transform the URL if matched, and skip rest processes like --rewrite[-host]-end, --proxy --return, etc.
  • --proxy executed if URL matched, and stop processing.
    • --header-add and --header-set executed if URL matched, and stop processing.
  • --return executed if URL matched, and stop processing.
    • --header-add and --header-set executed if URL matched, and stop processing.
    • --status-page executed if status code matched, and stop processing.
  • ghfs internal process
  • --header-add and --header-set executed if URL matched.
  • --to-status executed if URL matched, and stop processing.
    • --status-page executed if status code matched, and stop processing.
  • --status-page executed if status code matched, and stop processing.

Examples

Perform redirect according to redirect param:

# when requesting http://localhost:8080/redirect/www.example.com, redirect to https://www.example.com
ehfs -l 8080 -r /path/to/share --redirect '#/redirect/(.*)#https://$1'

Serve static page without .html suffix in URL:

  • redirect URL contains .html suffix to no suffix
  • rewrite URL without suffix to with .html suffix
ehfs -l 8080 -r /path/to/share --redirect '#(.*)\.html#$1' --rewrite-post '#^.*/[^/.]+$#$0.html'

Specify page for 404 status:

ehfs -l 8080 -r /path/to/share --status-page '#404#/path/to/404/file'

Refuse to serve for critical files or directories, returns 403 status:

ehfs -l 8080 -r /path/to/share --return '#.git|.htaccess#403'

Compile

Minimal required Go version is 1.18.

go build main.go

Will generate executable file "main" in current directory.

MIT License Copyright (c) 2022 MJ PC Lab Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

简单静态网站服务器,基于Go HTTP File Server(https://gitee.com/mjpclab/go-http-file-server)扩展。 expand collapse
Go and 3 more languages
MIT
Cancel

Releases (16)

All

Contributors

All

Activities

Load More
can not load any more
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/mjpclab/extra-http-file-server.git
git@gitee.com:mjpclab/extra-http-file-server.git
mjpclab
extra-http-file-server
extra-http-file-server
main

Search

344bd9b3 5694891 D2dac590 5694891