public_handers.go
package manager
import (
"net/http"
"regexp"
"strconv"
"io"
"strings"
"fmt"
"time"
)
var publicUrlRegex *regexp.Regexp
func init() {
var err error
publicUrlRegex, err = regexp.Compile("/([0-9]*)/([0-9]*)/(.*)")
if err != nil {
panic(err)
}
}
//公共资源处理器
func (vm *VolumeManager)publicEntry(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet, http.MethodHead:
if publicUrlRegex.MatchString(r.URL.Path) {
vm.publicReadFile(w, r)
} else {
http.NotFound(w, r)
}
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
//读取公共资源文件
func (vm *VolumeManager)publicReadFile(w http.ResponseWriter, r *http.Request) {
match := publicUrlRegex.FindStringSubmatch(r.URL.Path)
vid, _ := strconv.ParseUint(match[1], 10, 64)
volume := vm.Volumes[vid]
if volume == nil {
http.Error(w, "can't find volume", http.StatusNotFound)
return
}
fid, _ := strconv.ParseUint(match[2], 10, 64)
file, err := volume.Get(fid)
if err != nil || file.Info.FileName != match[3] {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", get_content_type(file.Info.FileName))
w.Header().Set("Accept-Ranges", "bytes")
w.Header().Set("ETag", fmt.Sprintf("\"%d\"", fid))
//暂时不使用Last-Modified,用ETag即可
//w.Header().Set("Last-Modified", file.Info.Mtime.Format(http.TimeFormat))
w.Header().Set("Expires", time.Now().In(time.UTC).Add(DefaultExpires).Format(http.TimeFormat))
etagMatch := false
if r.Header.Get("If-None-Match") != "" {
s := r.Header.Get("If-None-Match")
if etag, err := strconv.ParseUint(s[1:len(s) - 1], 10, 64); err == nil && etag == fid {
etagMatch = true
}
}
if r.Header.Get("Range") != "" {
ranges := strings.Split(r.Header.Get("Range")[6:], "-")
start, err := strconv.ParseUint(ranges[0], 10, 64)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
length := uint64(0)
if start > file.Info.Size {
start = file.Info.Size
} else if ranges[1] != "" {
end, err := strconv.ParseUint(ranges[1], 10, 64)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
end += 1
if end > file.Info.Size {
end = file.Info.Size
}
length = end - start
} else {
length = file.Info.Size - start
}
w.Header().Set("Content-Length", strconv.FormatUint(length, 10))
if length == 0 {
w.WriteHeader(http.StatusRequestedRangeNotSatisfiable)
return
}
w.Header().Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", start, start + length - 1, file.Info.Size))
if etagMatch {
w.WriteHeader(http.StatusNotModified)
return
}
w.WriteHeader(http.StatusPartialContent)
if r.Method != http.MethodHead {
file.Seek(int64(start), 0)
io.CopyN(w, file, int64(length))
}
} else {
w.Header().Set("Content-Length", strconv.FormatUint(file.Info.Size, 10))
if etagMatch {
w.WriteHeader(http.StatusNotModified)
} else if r.Method != http.MethodHead {
io.Copy(w, file)
}
}
}
public_handers.go的更多相关文章
随机推荐
- RHEL6 不重启扫描新添加硬盘
First find your host bus number grep mpt /sys/class/scsi_host/host?/proc_name Which should return a ...
- azkaban的安装部署
一.所需环境 1,JDK 2,HADOOP 4,mysql 3,HIVE 二.安装azkaban 1,安装git命令: yum install git 2,下载azkaban源码:git clone ...
- Ajax的简单使用
仅介绍Ajax的使用,让入门小白快速上手 //请自行引入jQuery库文件 <script type="text/javascript"> $(function() { ...
- Tracert(跟踪路由)是路由跟踪实用程序,用于确定 IP 数据包访问目标所采取的路径。
Tracert(跟踪路由)是路由跟踪实用程序,用于确定 IP 数据包访问目标所采取的路径. Tracert 命令用 IP 生存时间 (TTL) 字段和 ICMP 错误消息来确定从一个主机到网络上其 ...
- TensorFlow实践笔记(一):数据读取
本文整理了TensorFlow中的数据读取方法,在TensorFlow中主要有三种方法读取数据: Feeding:由Python提供数据. Preloaded data:预加载数据. Reading ...
- redis资源未释放引发的问题
一.redis资源未释放的起因: N年前,在修改一个古老程序时,不小心把redis释放的这块给干掉了, if (jedis != null) { if (!isInProcess) { jedis.d ...
- Oracle入门《Oracle介绍》第一章1-3 Oracle 逻辑组件
一.数据库的逻辑结构是从逻辑的角度分析数据库的组成.Oracle 的逻辑组件包括: 1.表空间 表空间是数据库中最大的逻辑单位,一个 Oracle 数据库至少包含一个表空间,就是名为SYSTEM的系统 ...
- html5 兼容版本 video
<!-- first try HTML5 playback: if serving as XML, expand `controls` to `controls="controls&q ...
- 同一台电脑上配置多个解压版tomcat方法(本例安装两个)
一.在环境变量中设置变量() CATALINA_HOME = tomcat路径一 CATALINA_BASE = tomcat路径一 CATALINA_HOME2 = tomcat路径二 CATALI ...
- Robot Framework自动化_Selenium2Library 关键字
Robot Framework自动化_Selenium2Library 关键字 培训老师:肖能尤 2016/06/12 课程目的 一.Robot framework Selenium2Library ...