5
0
Fork 0

Publish raw key instead of constructed key

master
UnicodingUnicorn 2019-02-06 14:09:49 +08:00
parent 3f8f28dd99
commit fec6e3173d
2 changed files with 12 additions and 40 deletions

View File

@ -21,7 +21,7 @@ var _ = math.Inf
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
type Bite struct { type Bite struct {
Key []byte `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"` Key string `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"`
Start uint64 `protobuf:"varint,2,opt,name=start,proto3" json:"start,omitempty"` Start uint64 `protobuf:"varint,2,opt,name=start,proto3" json:"start,omitempty"`
Data []byte `protobuf:"bytes,3,opt,name=data,proto3" json:"data,omitempty"` Data []byte `protobuf:"bytes,3,opt,name=data,proto3" json:"data,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_NoUnkeyedLiteral struct{} `json:"-"`
@ -54,11 +54,11 @@ func (m *Bite) XXX_DiscardUnknown() {
var xxx_messageInfo_Bite proto.InternalMessageInfo var xxx_messageInfo_Bite proto.InternalMessageInfo
func (m *Bite) GetKey() []byte { func (m *Bite) GetKey() string {
if m != nil { if m != nil {
return m.Key return m.Key
} }
return nil return ""
} }
func (m *Bite) GetStart() uint64 { func (m *Bite) GetStart() uint64 {
@ -82,12 +82,12 @@ func init() {
func init() { proto.RegisterFile("bite.proto", fileDescriptor_e1ec993646b17549) } func init() { proto.RegisterFile("bite.proto", fileDescriptor_e1ec993646b17549) }
var fileDescriptor_e1ec993646b17549 = []byte{ var fileDescriptor_e1ec993646b17549 = []byte{
// 102 bytes of a gzipped FileDescriptorProto // 105 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x4a, 0xca, 0x2c, 0x49, 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x4a, 0xca, 0x2c, 0x49,
0xd5, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0xc9, 0x4d, 0xcc, 0xcc, 0x53, 0x72, 0xe2, 0x62, 0xd5, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0xc9, 0x4d, 0xcc, 0xcc, 0x53, 0x72, 0xe2, 0x62,
0x71, 0xca, 0x2c, 0x49, 0x15, 0x12, 0xe0, 0x62, 0xce, 0x4e, 0xad, 0x94, 0x60, 0x54, 0x60, 0xd4, 0x71, 0xca, 0x2c, 0x49, 0x15, 0x12, 0xe0, 0x62, 0xce, 0x4e, 0xad, 0x94, 0x60, 0x54, 0x60, 0xd4,
0xe0, 0x09, 0x02, 0x31, 0x85, 0x44, 0xb8, 0x58, 0x8b, 0x4b, 0x12, 0x8b, 0x4a, 0x24, 0x98, 0x14, 0xe0, 0x0c, 0x02, 0x31, 0x85, 0x44, 0xb8, 0x58, 0x8b, 0x4b, 0x12, 0x8b, 0x4a, 0x24, 0x98, 0x14,
0x18, 0x35, 0x58, 0x82, 0x20, 0x1c, 0x21, 0x21, 0x2e, 0x96, 0x94, 0xc4, 0x92, 0x44, 0x09, 0x66, 0x18, 0x35, 0x58, 0x82, 0x20, 0x1c, 0x21, 0x21, 0x2e, 0x96, 0x94, 0xc4, 0x92, 0x44, 0x09, 0x66,
0xb0, 0x42, 0x30, 0x3b, 0x89, 0x0d, 0x6c, 0xa0, 0x31, 0x20, 0x00, 0x00, 0xff, 0xff, 0xa3, 0xe1, 0x05, 0x46, 0x0d, 0x9e, 0x20, 0x30, 0x3b, 0x89, 0x0d, 0x6c, 0xa0, 0x31, 0x20, 0x00, 0x00, 0xff,
0xe5, 0xfe, 0x5e, 0x00, 0x00, 0x00, 0xff, 0x28, 0xbd, 0x24, 0x95, 0x5e, 0x00, 0x00, 0x00,
} }

38
main.go
View File

@ -1,8 +1,6 @@
package main; package main;
import ( import (
"encoding/binary"
"errors"
"flag" "flag"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -47,11 +45,6 @@ func main() {
log.Fatal(http.ListenAndServe(listen, router)) log.Fatal(http.ListenAndServe(listen, router))
} }
// Marshalling keys and assorted helper functions
func validObj(obj string) bool {
return obj == "bite" || obj == "user"
}
// TODO: ensure security of regexp // TODO: ensure security of regexp
var validConversationRegexp = regexp.MustCompile(`^[a-zA-Z0-9-]+$`) var validConversationRegexp = regexp.MustCompile(`^[a-zA-Z0-9-]+$`)
@ -59,28 +52,6 @@ func validConversation(conversation string) bool {
return validConversationRegexp.MatchString(conversation) return validConversationRegexp.MatchString(conversation)
} }
const conversationSeprator = '@'
const objSeprator = '+'
func MarshalKey(obj, conversation string, start uint64) ([]byte, error) {
prefixBytes, err := MarshalKeyPrefix(obj, conversation)
if err != nil {
return nil, err
}
startBytes := make([]byte, 8)
binary.BigEndian.PutUint64(startBytes, start)
return append(prefixBytes, startBytes...), nil
}
func MarshalKeyPrefix(obj, conversation string) ([]byte, error) {
if !validObj(obj) || !validConversation(conversation) {
return nil, errors.New("main: FormatKey: bad obj or conversation")
}
return []byte(obj + string(objSeprator) + conversation + string(conversationSeprator)), nil
}
func ParseStartString(start string) (uint64, error) { func ParseStartString(start string) (uint64, error) {
return strconv.ParseUint(start, 10, 64) return strconv.ParseUint(start, 10, 64)
} }
@ -92,8 +63,9 @@ func PutBite(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return return
} }
key, err := MarshalKey("bite", p.ByName("key"), start)
if err != nil { key := p.ByName("key")
if !validConversation(key) {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return return
} }
@ -128,8 +100,8 @@ func PutBiteUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
return return
} }
key, err := MarshalKey("user", p.ByName("key"), start) key := p.ByName("key")
if err != nil { if !validConversation(key) {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return return
} }