-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
271 lines (223 loc) · 5.85 KB
/
main.go
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package main
import (
"bufio"
"bytes"
"context"
"encoding/json"
"io"
"lab1DS/sem"
"log"
"mime/multipart"
"net"
"net/http"
"os"
"strings"
)
type StdResponse struct {
Code int
Error bool
Message string
}
var filePath = os.Getenv("FILE_PATH")
func main() {
port := os.Args[1]
startServer(port)
}
/*
*
Start the webserver, and begin listening for incoming clients.
*/
func startServer(port string) {
var bindAddr string
/*
If environment variables above are not set, it means we are running locally, set the file path and bind address accordingly
*/
if filePath == "" {
filePath = "/Users/karthik/Public/tmp/"
bindAddr = "localhost"
} else {
bindAddr = "0.0.0.0"
}
log.Println("Server starting on port: " + port)
server, err := net.Listen("tcp", bindAddr+":"+port)
if err != nil {
log.Fatal("Error starting server: " + err.Error())
}
weightedSem := sem.NewWeighted(10) //Semaphore to ensure no more than 10 concurrent clients.
for {
semErr := weightedSem.Acquire(context.Background(), 1) //Grab one per client
client, err := server.Accept()
if err != nil || semErr != nil {
log.Println("Error accepting new client: " + err.Error())
return
} else {
go processClient(client, weightedSem)
}
}
}
/*
*
Function to handle incoming requests.
This function is always called on separate go-routines, and handles all the client data.
*/
func processClient(client net.Conn, weighted *sem.Weighted) {
log.Println("New client accepted,", client.RemoteAddr())
defer weighted.Release(1)
defer func(client net.Conn) {
err := client.Close()
if err != nil {
sendResponse(http.StatusInternalServerError, false, "Error Closing conn", client)
}
}(client)
reader := bufio.NewReader(client)
req, err := http.ReadRequest(reader) // Parse the http-request
if err != nil {
log.Println("Error building request: ", err)
return
}
log.Println("URL path: " + req.URL.Path)
log.Println("Request method: " + req.Method)
url := req.URL.Path
urlSlices := strings.Split(url, "/")
resourceName := urlSlices[len(urlSlices)-1]
/*
This if statement distinguishes between the different HTTP methods, and handles each appropriate
*/
/*------------------------GET---------------------*/
if req.Method == "GET" { // handle get
getHandler(resourceName, client, req)
sendResponse(http.StatusNotFound, true, "Please enter the right file name or upload a file", client)
/*------------------------POST---------------------*/
} else if req.Method == "POST" { //handle post
postHandler(req, client)
} else {
sendResponse(http.StatusNotImplemented, true, "Only GET and POST methods are supported", client)
}
}
func getHandler(resourceName string, client net.Conn, req *http.Request) {
if resourceName != "" {
sliceFileName := strings.Split(resourceName, ".")
ext := sliceFileName[len(sliceFileName)-1]
allowedExts := [6]string{"html", "txt", "gif", "jpeg", "jpg", "css"}
i := -1
for i < len(allowedExts) {
i++
if i == len(allowedExts) {
sendResponse(http.StatusNotImplemented, true, ext+" is not allowed", client)
return
}
if ext == allowedExts[i] {
break
}
}
file, err := os.ReadFile(filePath + "" + resourceName + "")
mimeType := http.DetectContentType(file) // Get Content-Type for adding it to the header
if err != nil {
log.Println(err)
sendResponse(400, true, err.Error(), client)
return
} else {
req.Header = make(http.Header)
req.Header.Set("Content-Type", mimeType) // Add the above Content-Type to the header
var res = http.Response{Proto: "HTTP/1.0",
ProtoMajor: 1,
ProtoMinor: 0,
Close: true,
StatusCode: 200,
Body: io.NopCloser(bytes.NewBuffer(file)),
Header: req.Header}
err := res.Write(client)
if err != nil {
log.Println(err)
return
}
}
}
}
func postHandler(req *http.Request, client net.Conn) {
retVal := multipartUpload(req)
var respCode = 0
var msg = "File uploaded"
if retVal {
respCode = http.StatusCreated
} else {
respCode = http.StatusBadRequest
msg = "Wrong File format or incomplete upload"
}
sendResponse(respCode, !retVal, msg, client)
}
func multipartUpload(req *http.Request) bool {
reader, err := req.MultipartReader()
if err != nil {
log.Println(err)
return false
}
for {
part, err := reader.NextPart()
if err == io.EOF {
break
} else if err != nil {
break
}
defer func(part *multipart.Part) {
err := part.Close()
if err != nil {
log.Println(err)
return
}
}(part)
if part.FileName() == "" {
continue
}
sliceFileName := strings.Split(part.FileName(), ".")
ext := sliceFileName[len(sliceFileName)-1]
allowedExts := [6]string{"html", "txt", "gif", "jpeg", "jpg", "css"}
i := -1
for i < len(allowedExts) {
i++
if i == len(allowedExts) { // If not in allowed extensions
return false // This will throw a 400 Bad request error
}
if ext == allowedExts[i] {
break
}
}
d, err := os.Create(filePath + part.FileName()) // Create a file in the path with given file name on the filesystem
if err != nil {
log.Println(err)
return false
}
defer func(d *os.File) {
err := d.Close()
if err != nil {
return
}
}(d)
_, err = io.Copy(d, part) // Copy the file from request to the newly created file on the the file system
if err != nil {
return false
}
}
return true // This will throw a 201 Created Response
}
/*
*
Function for sending responses to the client.
*/
func sendResponse(code int, error bool, message string, client net.Conn) {
jsonifiedStr, err := json.Marshal(StdResponse{Code: code, Error: error, Message: message})
if err != nil {
log.Println(err)
return
}
var res = http.Response{Proto: "HTTP/1.0",
ProtoMajor: 1,
ProtoMinor: 0,
Close: true,
StatusCode: code,
Body: io.NopCloser(bytes.NewReader(jsonifiedStr))}
err = res.Write(client)
if err != nil {
return
}
}