Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions core/src/main/java/spotty/common/http/HttpHeaders.java
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public HttpHeaders(HttpHeaders headers) {
/**
* add header
*
* @param name header name
* @param name header name
* @param value header value
* @return this instance of headers
*/
Expand Down Expand Up @@ -360,39 +360,39 @@ public String get(String name) {
* remove header by name
*
* @param name header name
* @return the previous header value associated with <tt>name</tt>, or
* <tt>null</tt> if there was no header for given <tt>name</tt>.
* @return the previous header value associated with name, or
* null if there was no header for given name.
*/
public String remove(String name) {
return headers.remove(name);
}

/**
* Returns <tt>true</tt> if this HttpHeaders contains a header for the specified name.
* Returns true if this HttpHeaders contains a header for the specified name.
*
* @param name header name
* @return <tt>true</tt> if this HttpHeaders contains a header for the specified name.
* @return true if this HttpHeaders contains a header for the specified name.
*/
public boolean has(String name) {
return headers.containsKey(name);
}

/**
* Returns <tt>false</tt> if this HttpHeaders contains no header for the specified name.
* Returns false if this HttpHeaders contains no header for the specified name.
*
* @param name header name
* @return <tt>true</tt> if this HttpHeaders contains no header for the specified name.
* @return true if this HttpHeaders contains no header for the specified name.
*/
public boolean hasNot(String name) {
return !headers.containsKey(name);
}

/**
* Returns <tt>true</tt> if this HttpHeaders contains a header for the specified name and header value is equal with given.
* Returns true if this HttpHeaders contains a header for the specified name and header value is equal with given.
*
* @param name header name
* @param name header name
* @param value header value
* @return <tt>true</tt> if this HttpHeaders contains a header for the specified name and header value is equal with given.
* @return true if this HttpHeaders contains a header for the specified name and header value is equal with given.
*/
public boolean hasAndEqual(String name, String value) {
final String header = headers.get(name);
Expand All @@ -413,18 +413,18 @@ public int size() {
}

/**
* Returns <tt>true</tt> if this HttpHeaders contains no headers.
* Returns true if this HttpHeaders contains no headers.
*
* @return <tt>true</tt> if this HttpHeaders contains no headers
* @return true if this HttpHeaders contains no headers
*/
public boolean isEmpty() {
return headers.isEmpty();
}

/**
* Returns <tt>true</tt> if this HttpHeaders contains headers.
* Returns true if this HttpHeaders contains headers.
*
* @return <tt>true</tt> if this HttpHeaders contains headers
* @return true if this HttpHeaders contains headers
*/
public boolean isNotEmpty() {
return headers.size() > 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2022 - Alex Danilenko
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package spotty.common.response;

import spotty.common.http.HttpHeaders;
import spotty.common.stream.output.SpottyByteArrayOutputStream;

import static java.nio.charset.StandardCharsets.UTF_8;

public final class ResponseHeadersWriter {
private static final byte[] HEADER_SPLITTER = ": ".getBytes(UTF_8);
private static final byte[] SPACE = " ".getBytes(UTF_8);
private static final byte[] CONTENT_LENGTH = HttpHeaders.CONTENT_LENGTH.getBytes(UTF_8);
private static final byte[] CONTENT_TYPE = HttpHeaders.CONTENT_TYPE.getBytes(UTF_8);
private static final byte[] SET_COOKIE = HttpHeaders.SET_COOKIE.getBytes(UTF_8);

public static void write(SpottyByteArrayOutputStream writer, SpottyResponse response) {
writer.print(response.protocol().code); writer.write(SPACE); writer.print(response.status().toString());
writer.println();

writer.write(CONTENT_LENGTH); writer.write(HEADER_SPLITTER); writer.print(Integer.toString(response.contentLength()));
writer.println();

if (response.contentType() != null) {
writer.write(CONTENT_TYPE); writer.write(HEADER_SPLITTER); writer.print(response.contentType());
writer.println();
}

response.headers()
.forEach((name, value) -> {
writer.print(name); writer.write(HEADER_SPLITTER); writer.print(value);
writer.println();
});

response.cookies()
.forEach(cookie -> {
writer.write(SET_COOKIE); writer.write(HEADER_SPLITTER); writer.print(cookie.toString());
writer.println();
});

writer.println();
}

}
72 changes: 0 additions & 72 deletions core/src/main/java/spotty/common/response/ResponseWriter.java

This file was deleted.

24 changes: 12 additions & 12 deletions core/src/main/java/spotty/common/session/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public Session putIfAbsent(Object key, Object value) {
*
* @param key key with which the specified value is to be associated
* @param mapper the function to compute a value
* @param <T> return type
* @param <T> return type
* @return the current (existing or computed) value associated with
* the specified key, or null if the computed value is null
*/
Expand All @@ -132,7 +132,7 @@ public <T> T computeIfAbsent(Object key, Function<Object, T> mapper) {
*
* @param key key with which the specified value is to be associated
* @param mapper the function to compute a value
* @param <T> return type
* @param <T> return type
* @return the current (existing or computed) value associated with
* the specified key, or null if the computed value is null
*/
Expand All @@ -154,7 +154,7 @@ public <T> T computeIfPresent(Object key, BiFunction<Object, T, T> mapper) {
*
* @param key key with which the specified value is to be associated
* @param mapper the function to compute a value
* @param <T> return type
* @param <T> return type
* @return the new value associated with the specified key, or null if none
*/
@SuppressWarnings("all")
Expand Down Expand Up @@ -192,7 +192,7 @@ public <T> T get(Object key) {
*
* @param key the key whose associated value is to be returned
* @param defaultValue the default mapping of the key
* @param <T> return type
* @param <T> return type
* @return the value to which the specified key is mapped, or
* {@code defaultValue} if this map contains no mapping for the key
*/
Expand All @@ -209,14 +209,14 @@ public int size() {
}

/**
* @return <tt>true</tt> if this session contains no key-value mappings
* @return true if this session contains no key-value mappings
*/
public boolean isEmpty() {
return data.isEmpty();
}

/**
* @return <tt>true</tt> if this session contains key-value mappings
* @return true if this session contains key-value mappings
*/
public boolean isNotEmpty() {
return data.size() > 0;
Expand Down Expand Up @@ -271,31 +271,31 @@ public Set<Map.Entry<Object, Object>> entrySet() {
}

/**
* Returns <tt>true</tt> if this session contains a mapping for the specified key.
* Returns true if this session contains a mapping for the specified key.
*
* @param key key whose presence in this session is to be tested
* @return <tt>true</tt> if this session contains a mapping for the specified key
* @return true if this session contains a mapping for the specified key
*/
public boolean has(Object key) {
return data.containsKey(key);
}

/**
* Returns <tt>true</tt> if this session contains no mapping for the specified key.
* Returns true if this session contains no mapping for the specified key.
*
* @param key key whose presence in this session is to be tested
* @return <tt>true</tt> if this session contains no mapping for the specified key
* @return true if this session contains no mapping for the specified key
*/
public boolean hasNot(Object key) {
return !data.containsKey(key);
}

/**
* Returns <tt>true</tt> if this session contains a value for the specified key and value is equal with given.
* Returns true if this session contains a value for the specified key and value is equal with given.
*
* @param key key whose presence in this session is to be tested
* @param value value whose presence in this session is to be tested
* @return <tt>true</tt> if this session contains a value for the specified key and value is equal with given.
* @return true if this session contains a value for the specified key and value is equal with given.
*/
public boolean hasAndEqual(Object key, Object value) {
final Object foundValue = data.get(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,15 @@ public boolean isFull() {
return size == limit;
}

public byte[] sourceData() {
return data;
}

public byte[] toByteArray() {
if (size == data.length) {
return data;
}

return Arrays.copyOf(data, size);
}

Expand All @@ -115,7 +123,7 @@ public void capacity(int capacity) {
}

if (data.length != capacity) {
byte[] d = new byte[capacity];
final byte[] d = new byte[capacity];
this.size = min(size, capacity);

if (size > 0) {
Expand Down
1 change: 1 addition & 0 deletions core/src/main/java/spotty/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ private void accept(SelectionKey acceptKey) throws IOException {
private void registerConnection(Connection connection, Selector selector) {
final SelectionKey key = connection.register(selector);
if (key == null) {
connection.close();
return;
}

Expand Down
Loading