greenbender / pynntp

Python NNTP Library (supports compression)
GNU General Public License v3.0
19 stars 4 forks source link

Supporting COMPRESS command #41

Open Julien-Elie opened 1 month ago

Julien-Elie commented 1 month ago

The library currently implements XZHDR, XZVER and XFEATURE COMPRESS GZIP. Would it be possible to also implement the COMPRESS extension? (RFC 8054)

Quick-and-dirty example of use in Python:

import socket
import zlib

HOST = 'news.trigofacile.com'
PORT = 119
RESPONSE_LENGTH = 2 ** 20

METHOD = zlib.DEFLATED
WINDOW_SIZE = -15
MEMLEVEL = 9
COMPLEVEL = 9
STRATEGY = zlib.Z_DEFAULT_STRATEGY

encoder = zlib.compressobj(COMPLEVEL, METHOD, WINDOW_SIZE, MEMLEVEL, STRATEGY)
decoder = zlib.decompressobj(WINDOW_SIZE)

def sendcompress(command, repeat=0):
    zdata = encoder.compress(command + '\r\n')
    zdata += encoder.flush(zlib.Z_SYNC_FLUSH)
    print "[C] %s" % command
    s.send(zdata)

    while repeat >= 0:
        zdata = s.recv(RESPONSE_LENGTH)
        data = decoder.decompress(zdata)
        print "[S] %s" % repr(data)[1:-5].replace('\\r\\n', '\n[S] ')
        repeat -= 1

def send(command):
    if command:
        print "[C] %s" % command
        s.send(command + '\r\n')
    data = s.recv(RESPONSE_LENGTH)
    print "[S] %s" % repr(data)[1:-5].replace('\\r\\n', '\n[S] ')

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))

send(None)
send("COMPRESS DEFLATE")
sendcompress("DATE")
sendcompress("GROUP trigofacile.test")
sendcompress("CAPABILITIES", 1)
sendcompress("QUIT")
s.close()

results in:

[S] 200 news.trigofacile.com InterNetNews NNRP server INN 2.8.0 (20240722 prerelease) ready (posting ok)
[C] COMPRESS DEFLATE
[S] 206 Compression now active; enjoy the speed!
[C] DATE
[S] 111 20240927193114
[C] GROUP trigofacile.test
[S] 211 644 7 754 trigofacile.test
[C] CAPABILITIES
[S] 101 Capability list:
[S] VERSION 2
[S] IMPLEMENTATION INN 2.8.0 (20240722 prerelease)
[S] AUTHINFO
[S] HDR
[S] LIST ACTIVE ACTIVE.TIMES COUNTS DISTRIB.PATS DISTRIBUTIONS HEADERS MODERATORS MOTD NEWSGROUPS OVERVIEW.FMT SUBSCRIPTIONS
[S] NEWNEWS
[S] OVER
[S] POST
[S] READER
[S] SASL GSS-SPNEGO GSSAPI SCRAM-SHA-512 SCRAM-SHA-384 SCRAM-SHA-256 SCRAM-SHA-224 SCRAM-SHA-1 GS2-KRB5 GS2-IAKERB DIGEST-MD5 OTP NTLM CRAM-MD5 LOGIN PLAIN
[S] XPAT
[S] .
[C] QUIT
[S] 205 Bye!