File tree Expand file tree Collapse file tree 2 files changed +78
-4
lines changed Expand file tree Collapse file tree 2 files changed +78
-4
lines changed Original file line number Diff line number Diff line change
1
+ (ns echo.core
2
+ (:gen-class )
3
+ (:require [clojure.string :as cs]
4
+ [clojure.java.io :as io])
5
+ (:import org.apache.commons.codec.binary.Hex
6
+ java.io.BufferedReader
7
+ java.io.IOException
8
+ java.io.InputStreamReader))
9
+
10
+
11
+ (defn transform-http-msg
12
+ " Function that transforms/filters the incoming HTTP messages."
13
+ [headers body]
14
+ ; ; do actual transformations here
15
+ [headers body])
16
+
17
+
18
+ (defn decode-hex-string
19
+ " Decode an Hex-encoded string."
20
+ [s]
21
+ (String. (Hex/decodeHex (.toCharArray s))))
22
+
23
+
24
+ (defn encode-hex-string
25
+ " Encode a string to a hex-encoded string."
26
+ [^String s]
27
+ (String. (Hex/encodeHex (.getBytes s))))
28
+
29
+
30
+ (defn -main
31
+ [& args]
32
+ (let [br (BufferedReader. (InputStreamReader. System/in))]
33
+ (try
34
+ (loop [hex-line (.readLine br)]
35
+ (let [decoded-req (decode-hex-string hex-line)
36
+
37
+ ; ; empty line separates headers from body
38
+ http-request (partition-by empty? (cs/split-lines decoded-req))
39
+ headers (first http-request)
40
+
41
+ ; ; HTTP messages can contain no body:
42
+ body (when (= 3 (count http-request)) (last http-request))
43
+ [new-headers new-body] (transform-http-msg headers body)]
44
+
45
+ (println (encode-hex-string (str (cs/join " \n " headers)
46
+ (when body
47
+ (str " \n\n "
48
+ (cs/join " \n " body)))))))
49
+ (when-let [line (.readLine br)]
50
+ (recur line)))
51
+ (catch IOException e nil ))))
52
+
53
+
Original file line number Diff line number Diff line change 2
2
import java .io .IOException ;
3
3
import java .io .InputStreamReader ;
4
4
5
- public class echo {
6
- public static void main (String [] args ) {
5
+ import org .apache .commons .codec .DecoderException ;
6
+ import org .apache .commons .codec .binary .Hex ;
7
+
8
+
9
+ public class Echo {
10
+ public static String decodeHexString (String s ) throws DecoderException {
11
+ return new String (Hex .decodeHex (s .toCharArray ()));
12
+ }
13
+
14
+ public static String encodeHexString (String s ) {
15
+ return new String (Hex .encodeHex (s .getBytes ()));
16
+ }
17
+
18
+ public static String transformHTTPMessage (String req ) {
19
+ // do actual transformations here
20
+ return req ;
21
+ }
22
+
23
+ public static void main (String [] args ) throws DecoderException {
7
24
if (args != null ){
8
25
for (String arg : args ){
9
26
System .out .println (arg );
@@ -17,11 +34,15 @@ public static void main(String[] args) {
17
34
18
35
try {
19
36
while ((line = stdin .readLine ()) != null ) {
37
+ String decodedLine = decodeHexString (line );
38
+
39
+ String transformedLine = transformHTTPMessage (decodedLine );
20
40
21
- System .out .println (line );
41
+ String encodedLine = encodeHexString (transformedLine );
42
+ System .out .println (encodedLine );
22
43
23
44
}
24
45
} catch (IOException e ) {
25
46
}
26
47
}
27
- }
48
+ }
You can’t perform that action at this time.
0 commit comments