Skip to content

Instantly share code, notes, and snippets.

@cocreature
Last active December 14, 2019 21:06
Show Gist options
  • Save cocreature/14909476d7ea6a4b807a1f5aa227393a to your computer and use it in GitHub Desktop.
Save cocreature/14909476d7ea6a4b807a1f5aa227393a to your computer and use it in GitHub Desktop.
* Free form cobol is for hipsters, we use proper fixed form cobol.
IDENTIFICATION DIVISION.
PROGRAM-ID. gobl.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 hostname PIC X(10) VALUE "localhost" & x"00".
01 port BINARY-SHORT VALUE 7575.
01 gethostbyname-result POINTER SYNC.
01 hostent BASED.
03 hostent-h-name POINTER SYNC.
03 hostent-h-aliases POINTER SYNC.
03 hostent-h-addrtype BINARY-INT.
03 hostent-h-length BINARY-INT.
03 hostent-h-addr-list POINTER SYNC.
01 sockaddr-in.
03 sockaddr-in-family BINARY-SHORT UNSIGNED VALUE 2.
03 sockaddr-in-port BINARY-SHORT UNSIGNED.
03 sockaddr-in-addr BINARY-INT UNSIGNED.
03 foo BINARY-INT VALUE 0.
03 bar BINARY-INT VALUE 0.
01 first-address POINTER based.
01 addr BINARY-INT based.
01 socket-fd BINARY-INT.
01 family BINARY-INT VALUE 2.
01 socket-type BINARY-INT VALUE 1.
01 total BINARY-INT.
01 write-pos BINARY-INT VALUE 1.
01 bytes-to-write BINARY-INT.
01 read-bytes BINARY-INT VALUE 0.
01 read-buf PIC X(1024).
01 http-message PIC X(382) VALUE "POST /command/create " &
"HTTP/1.0" & x"0d0a" &
"Host: localhost:7575" & x"0d0a" &
"Authorization: Bearer ey" &
"JhbGciOiJIUzI1NiIsInR5cCI6I" &
"kpXVCJ9.eyJsZWRnZXJJZCI6Ik15T" &
"GVkZ2VyIiwiYXBwbGljYXRpb25JZCI6Im" &
"Zvb2JhciIsInBhcnR5IjoiQWxpY2UifQ.4H" &
"YfzjlYr1ApUDot0a6a4zB49zS_jrwRUOCkAiPMq" &
"o0" & x"0d0a" &
"Content-Type: application/json" & x"0d0a" &
"Content-Length: 84" & x"0d0a" &
x"0d0a" &
"{""templateId"":{""moduleName"":" &
"""Main"", ""entityName"": ""T""}, " &
"""argument"": {""p"": ""Alice""}}".
PROCEDURE DIVISION.
DISPLAY "Join the recommended path"
CALL 'gethostbyname' USING
BY CONTENT hostname
RETURNING gethostbyname-result
END-CALL
DISPLAY "gethostnameby: " gethostbyname-result
SET ADDRESS OF hostent TO gethostbyname-result
CALL 'htons' USING
BY VALUE port
RETURNING sockaddr-in-port
END-CALL
SET ADDRESS OF first-address to hostent-h-addr-list
SET ADDRESS of addr to first-address
MOVE addr TO sockaddr-in-addr
CALL 'socket' USING
BY VALUE 2
BY VALUE 1
BY VALUE 0
RETURNING socket-fd
END-CALL
DISPLAY "socket: " return-code
CALL 'connect' USING
BY VALUE socket-fd
BY REFERENCE sockaddr-in
BY VALUE 16
END-CALL
DISPLAY "connect: " return-code
MOVE LENGTH OF http-message TO total
PERFORM WRITE-PARA WITH TEST AFTER UNTIL write-pos>total
PERFORM READ-PARA WITH TEST AFTER UNTIL read-bytes=0.
STOP RUN.
WRITE-PARA.
COMPUTE bytes-to-write = LENGTH OF http-message - write-pos + 1
CALL 'write' USING
BY VALUE socket-fd
BY REFERENCE http-message(write-pos:LENGTH OF http-message)
BY VALUE bytes-to-write
END-CALL
COMPUTE write-pos = write-pos + return-code.
READ-PARA.
CALL 'read' USING
BY VALUE socket-fd
BY REFERENCE read-buf
BY VALUE 1024
RETURNING read-bytes
END-CALL
DISPLAY read-buf(1:read-bytes).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment