1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

all: disallow pointer arithmetic for AssignStmt, PostfixExpr outside unsafe{} (#5581)

This commit is contained in:
Nick Treleaven
2020-07-01 13:50:17 +01:00
committed by GitHub
parent 5eb76606ae
commit e7339fec15
6 changed files with 68 additions and 33 deletions

View File

@@ -1,12 +1,6 @@
module picohttpparser
[inline]
fn cpy_str(dst byteptr, src string) int {
C.memcpy(dst, src.str, src.len)
return src.len
}
[inline]
[inline] [unsafe_fn]
fn cpy(dst, src byteptr, len int) int {
C.memcpy(dst, src, len)
return len

View File

@@ -9,87 +9,99 @@ pub mut:
buf byteptr
}
[inline] [unsafe_fn]
fn (mut r Response) write_str(s string) {
unsafe {
C.memcpy(r.buf, s.str, s.len)
r.buf += s.len
}
}
[inline]
pub fn (mut r Response) http_ok() &Response {
r.buf += cpy_str(r.buf, "HTTP/1.1 200 OK\r\n")
r.write_str("HTTP/1.1 200 OK\r\n")
return r
}
[inline]
pub fn (mut r Response) header(k, v string) &Response {
r.buf += cpy_str(r.buf, k)
r.buf += cpy_str(r.buf, ": ")
r.buf += cpy_str(r.buf, v)
r.buf += cpy_str(r.buf, "\r\n")
r.write_str(k)
r.write_str(": ")
r.write_str(v)
r.write_str("\r\n")
return r
}
[inline]
pub fn (mut r Response) header_date() &Response {
r.buf += cpy_str(r.buf, "Date: ")
r.buf += cpy(r.buf, r.date, 29)
r.buf += cpy_str(r.buf, "\r\n")
r.write_str("Date: ")
unsafe {
r.buf += cpy(r.buf, r.date, 29)
}
r.write_str("\r\n")
return r
}
[inline]
pub fn (mut r Response) header_server() &Response {
r.buf += cpy_str(r.buf, "Server: V\r\n")
r.write_str("Server: V\r\n")
return r
}
[inline]
pub fn (mut r Response) content_type(s string) &Response {
r.buf += cpy_str(r.buf, "Content-Type: ")
r.buf += cpy_str(r.buf, s)
r.buf += cpy_str(r.buf, "\r\n")
r.write_str("Content-Type: ")
r.write_str(s)
r.write_str("\r\n")
return r
}
[inline]
pub fn (mut r Response) html() &Response {
r.buf += cpy_str(r.buf, "Content-Type: text/html\r\n")
r.write_str("Content-Type: text/html\r\n")
return r
}
[inline]
pub fn (mut r Response) plain() &Response {
r.buf += cpy_str(r.buf, "Content-Type: text/plain\r\n")
r.write_str("Content-Type: text/plain\r\n")
return r
}
[inline]
pub fn (mut r Response) json() &Response {
r.buf += cpy_str(r.buf, "Content-Type: application/json\r\n")
r.write_str("Content-Type: application/json\r\n")
return r
}
[inline]
pub fn (mut r Response) body(body string) {
r.buf += cpy_str(r.buf, "Content-Length: ")
r.buf += C.u64toa(r.buf, body.len)
r.buf += cpy_str(r.buf, "\r\n\r\n")
r.buf += cpy_str(r.buf, body)
r.write_str("Content-Length: ")
unsafe {
r.buf += C.u64toa(r.buf, body.len)
}
r.write_str("\r\n\r\n")
r.write_str(body)
}
[inline]
pub fn (mut r Response) http_404() {
r.buf += cpy_str(r.buf, 'HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n')
r.write_str('HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n')
}
[inline]
pub fn (mut r Response) http_405() {
r.buf += cpy_str(r.buf, 'HTTP/1.1 405 Method Not Allowed\r\nContent-Length: 0\r\n\r\n')
r.write_str('HTTP/1.1 405 Method Not Allowed\r\nContent-Length: 0\r\n\r\n')
}
[inline]
pub fn (mut r Response) http_500() {
r.buf += cpy_str(r.buf, 'HTTP/1.1 500 Internal Server Error\r\nContent-Length: 0\r\n\r\n')
r.write_str('HTTP/1.1 500 Internal Server Error\r\nContent-Length: 0\r\n\r\n')
}
[inline]
pub fn (mut r Response) raw(response string) {
r.buf += cpy_str(r.buf, response)
r.write_str(response)
}
[inline]