-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhttp.go
More file actions
165 lines (149 loc) · 4.76 KB
/
Copy pathhttp.go
File metadata and controls
165 lines (149 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright (C) 2021 CGI France
//
// This file is part of LINO.
//
// LINO is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// LINO is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with LINO. If not, see <http://www.gnu.org/licenses/>.
package push
import (
"fmt"
"html"
"math"
"net/http"
"strconv"
"github.com/cgi-fr/lino/pkg/push"
"github.com/gorilla/mux"
"github.com/rs/zerolog/log"
)
func DeleteHandlerFactory(ingressDescriptor string) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
Handler(w, r, push.Delete, ingressDescriptor)
}
}
func InsertHandlerFactory(ingressDescriptor string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
Handler(w, r, push.Insert, ingressDescriptor)
}
}
func TruncatHandlerFactory(ingressDescriptor string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
Handler(w, r, push.Truncate, ingressDescriptor)
}
}
func Handler(w http.ResponseWriter, r *http.Request, mode push.Mode, ingressDescriptor string) {
pathParams := mux.Vars(r)
query := r.URL.Query()
var (
dcDestination string
ok bool
commitSize = uint(100)
disableConstraints bool
)
if dcDestination, ok = pathParams["dataDestination"]; !ok {
log.Error().Msg("param dataDestination is required")
w.WriteHeader(http.StatusBadRequest)
_, ew := w.Write([]byte("{\"error\": \"param dataDestination is required\"}"))
if ew != nil {
log.Error().Err(ew).Msg("Write failed")
return
}
return
}
datadestination, err := getDataDestination(dcDestination)
if err != nil {
log.Error().Err(err).Msg("")
w.WriteHeader(http.StatusNotFound)
_, ew := w.Write([]byte("{\"error\": \"" + html.EscapeString(err.Description) + "\"}"))
if ew != nil {
log.Error().Err(ew).Msg("Write failed")
return
}
return
}
autoTruncate := false
if query.Get("auto-truncate") != "" {
var err error
autoTruncate, err = strconv.ParseBool(query.Get("auto-truncate"))
if err != nil {
log.Error().Err(err).Msg("can't parse auto-truncate")
w.WriteHeader(http.StatusBadRequest)
_, ew := w.Write([]byte("{\"error\" : \"param auto-truncate must be a boolean\"}\n"))
if ew != nil {
log.Error().Err(ew).Msg("Write failed")
return
}
return
}
}
plan, e2 := getPlan(idStorageFactory(query.Get("table"), ingressDescriptor), autoTruncate)
if e2 != nil {
log.Error().Err(e2).Msg("")
w.WriteHeader(http.StatusNotFound)
_, ew := w.Write([]byte("{\"error\": \"" + e2.Description + "\"}"))
if ew != nil {
log.Error().Err(ew).Msg("Write failed")
return
}
return
}
if query.Get("commitsize") != "" {
commitsize64, ecommitsize := strconv.ParseUint(query.Get("commitsize"), 10, 32)
if ecommitsize != nil {
log.Error().Err(ecommitsize).Msg("can't parse commitsize")
w.WriteHeader(http.StatusBadRequest)
_, ew := w.Write([]byte("{\"error\" : \"param commitsize must be an positive integer\"}\n"))
if ew != nil {
log.Error().Err(ew).Msg("Write failed")
return
}
return
}
// CWE-190 CWE-681
if commitsize64 <= math.MaxInt32 {
commitSize = uint(commitsize64)
} else {
commitSize = math.MaxInt32
}
}
if query.Get("disable-constraints") != "" {
var edisableConstraints error
disableConstraints, edisableConstraints = strconv.ParseBool(query.Get("disable-constraints"))
if edisableConstraints != nil {
log.Error().Err(edisableConstraints).Msg("can't parse disable-constraints")
w.WriteHeader(http.StatusBadRequest)
_, ew := w.Write([]byte("{\"error\" : \"param disable-constraints must be a boolean\"}\n"))
if ew != nil {
log.Error().Err(ew).Msg("Write failed")
return
}
return
}
}
log.Debug().Msg(fmt.Sprintf("call Push with mode %s", mode))
e3 := push.Push(rowIteratorFactory(r.Body), datadestination, plan, mode, commitSize, 0, disableConstraints, push.NoErrorCaptureRowWriter{}, nil, query.Get("using-pk-field"), "", "", false)
if e3 != nil {
log.Error().Err(e3).Msg("")
w.WriteHeader(http.StatusNotFound)
_, ew := w.Write([]byte("{\"error\": \"" + e3.Description + "\"}"))
if ew != nil {
log.Error().Err(ew).Msg("Write failed")
return
}
return
}
_, ew := w.Write([]byte("{\"error\": \"\"}"))
if ew != nil {
log.Error().Err(ew).Msg("Write failed")
return
}
}