pop3: Code tidy up before the introduction of authentication code

Moved EOB definition into header file.

Switched the logic around in pop3_endofresp() to allow for the
introduction of auth-mechanism detection.

Repositioned second and third function variables where they will fit
within the 78 character line limit.

Tidied up some comments.
This commit is contained in:
Steve Holme 2012-05-27 10:54:13 +01:00
parent f95f19e854
commit c6495bccbf
2 changed files with 45 additions and 61 deletions

View File

@ -207,26 +207,24 @@ static const struct Curl_handler Curl_handler_pop3s_proxy = {
#endif #endif
#endif #endif
/* function that checks for a pop3 status code at the start of the given /* Function that checks for an ending pop3 status code at the start of the
string */ given string */
static int pop3_endofresp(struct pingpong *pp, static int pop3_endofresp(struct pingpong *pp, int *resp)
int *resp)
{ {
char *line = pp->linestart_resp; char *line = pp->linestart_resp;
size_t len = pp->nread_resp; size_t len = pp->nread_resp;
if(((len >= 3) && !memcmp("+OK", line, 3)) || if((len < 3 || memcmp("+OK", line, 3)) &&
((len >= 4) && !memcmp("-ERR", line, 4))) { (len < 4 || memcmp("-ERR", line, 4)))
*resp = line[1]; /* O or E */ return FALSE; /* Nothing for us */
return TRUE;
}
return FALSE; /* nothing for us */ *resp = line[1]; /* O or E */
return TRUE;
} }
/* This is the ONLY way to change POP3 state! */ /* This is the ONLY way to change POP3 state! */
static void state(struct connectdata *conn, static void state(struct connectdata *conn, pop3state newstate)
pop3state newstate)
{ {
#if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS) #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
/* for debug purposes */ /* for debug purposes */
@ -267,8 +265,7 @@ static CURLcode pop3_state_user(struct connectdata *conn)
} }
/* For the POP3 "protocol connect" and "doing" phases only */ /* For the POP3 "protocol connect" and "doing" phases only */
static int pop3_getsock(struct connectdata *conn, static int pop3_getsock(struct connectdata *conn, curl_socket_t *socks,
curl_socket_t *socks,
int numsocks) int numsocks)
{ {
return Curl_pp_getsock(&conn->proto.pop3c.pp, socks, numsocks); return Curl_pp_getsock(&conn->proto.pop3c.pp, socks, numsocks);
@ -283,7 +280,7 @@ static void pop3_to_pop3s(struct connectdata *conn)
#define pop3_to_pop3s(x) Curl_nop_stmt #define pop3_to_pop3s(x) Curl_nop_stmt
#endif #endif
/* for the initial server greeting */ /* For the initial server greeting */
static CURLcode pop3_state_servergreet_resp(struct connectdata *conn, static CURLcode pop3_state_servergreet_resp(struct connectdata *conn,
int pop3code, int pop3code,
pop3state instate) pop3state instate)
@ -311,7 +308,7 @@ static CURLcode pop3_state_servergreet_resp(struct connectdata *conn,
return result; return result;
} }
/* for STARTTLS responses */ /* For STARTTLS responses */
static CURLcode pop3_state_starttls_resp(struct connectdata *conn, static CURLcode pop3_state_starttls_resp(struct connectdata *conn,
int pop3code, int pop3code,
pop3state instate) pop3state instate)
@ -345,7 +342,7 @@ static CURLcode pop3_state_starttls_resp(struct connectdata *conn,
return result; return result;
} }
/* for USER responses */ /* For USER responses */
static CURLcode pop3_state_user_resp(struct connectdata *conn, static CURLcode pop3_state_user_resp(struct connectdata *conn,
int pop3code, int pop3code,
pop3state instate) pop3state instate)
@ -372,7 +369,7 @@ static CURLcode pop3_state_user_resp(struct connectdata *conn,
return result; return result;
} }
/* for PASS responses */ /* For PASS responses */
static CURLcode pop3_state_pass_resp(struct connectdata *conn, static CURLcode pop3_state_pass_resp(struct connectdata *conn,
int pop3code, int pop3code,
pop3state instate) pop3state instate)
@ -392,7 +389,7 @@ static CURLcode pop3_state_pass_resp(struct connectdata *conn,
return result; return result;
} }
/* for the command response */ /* For the command response */
static CURLcode pop3_state_command_resp(struct connectdata *conn, static CURLcode pop3_state_command_resp(struct connectdata *conn,
int pop3code, int pop3code,
pop3state instate) pop3state instate)
@ -447,7 +444,7 @@ static CURLcode pop3_state_command_resp(struct connectdata *conn,
return result; return result;
} }
/* start the DO phase for the command */ /* Start the DO phase for the command */
static CURLcode pop3_command(struct connectdata *conn) static CURLcode pop3_command(struct connectdata *conn)
{ {
CURLcode result = CURLE_OK; CURLcode result = CURLE_OK;
@ -537,7 +534,7 @@ static CURLcode pop3_statemach_act(struct connectdata *conn)
return result; return result;
} }
/* called repeatedly until done from multi.c */ /* Called repeatedly until done from multi.c */
static CURLcode pop3_multi_statemach(struct connectdata *conn, bool *done) static CURLcode pop3_multi_statemach(struct connectdata *conn, bool *done)
{ {
struct pop3_conn *pop3c = &conn->proto.pop3c; struct pop3_conn *pop3c = &conn->proto.pop3c;
@ -563,10 +560,8 @@ static CURLcode pop3_easy_statemach(struct connectdata *conn)
return result; return result;
} }
/* /* Allocate and initialize the POP3 struct for the current SessionHandle if
* Allocate and initialize the struct POP3 for the current SessionHandle. If required */
* need be.
*/
static CURLcode pop3_init(struct connectdata *conn) static CURLcode pop3_init(struct connectdata *conn)
{ {
struct SessionHandle *data = conn->data; struct SessionHandle *data = conn->data;
@ -591,16 +586,18 @@ static CURLcode pop3_init(struct connectdata *conn)
return CURLE_OK; return CURLE_OK;
} }
/* /***********************************************************************
* pop3_connect() should do everything that is to be considered a part of *
* the connection phase. * pop3_connect()
*
* This function should do everything that is to be considered a part of the
* connection phase.
* *
* The variable 'done' points to will be TRUE if the protocol-layer connect * The variable 'done' points to will be TRUE if the protocol-layer connect
* phase is done when this function returns, or FALSE is not. When called as * phase is done when this function returns, or FALSE is not. When called as
* a part of the easy interface, it will always be TRUE. * a part of the easy interface, it will always be TRUE.
*/ */
static CURLcode pop3_connect(struct connectdata *conn, static CURLcode pop3_connect(struct connectdata *conn, bool *done)
bool *done) /* see description above */
{ {
CURLcode result; CURLcode result;
struct pop3_conn *pop3c = &conn->proto.pop3c; struct pop3_conn *pop3c = &conn->proto.pop3c;
@ -698,11 +695,8 @@ static CURLcode pop3_done(struct connectdata *conn, CURLcode status,
* This is the actual DO function for POP3. Get a file/directory according to * This is the actual DO function for POP3. Get a file/directory according to
* the options previously setup. * the options previously setup.
*/ */
static CURLcode pop3_perform(struct connectdata *conn, bool *connected,
static bool *dophase_done)
CURLcode pop3_perform(struct connectdata *conn,
bool *connected, /* connect status after PASV / PORT */
bool *dophase_done)
{ {
/* this is POP3 and no proxy */ /* this is POP3 and no proxy */
CURLcode result = CURLE_OK; CURLcode result = CURLE_OK;
@ -785,7 +779,6 @@ static CURLcode pop3_do(struct connectdata *conn, bool *done)
* This should be called before calling sclose(). We should then wait for the * This should be called before calling sclose(). We should then wait for the
* response from the server before returning. The calling code should then try * response from the server before returning. The calling code should then try
* to close the connection. * to close the connection.
*
*/ */
static CURLcode pop3_quit(struct connectdata *conn) static CURLcode pop3_quit(struct connectdata *conn)
{ {
@ -861,8 +854,7 @@ static CURLcode pop3_parse_custom_request(struct connectdata *conn)
} }
/* call this when the DO phase has completed */ /* call this when the DO phase has completed */
static CURLcode pop3_dophase_done(struct connectdata *conn, static CURLcode pop3_dophase_done(struct connectdata *conn, bool connected)
bool connected)
{ {
struct FTP *pop3 = conn->data->state.proto.pop3; struct FTP *pop3 = conn->data->state.proto.pop3;
@ -876,8 +868,7 @@ static CURLcode pop3_dophase_done(struct connectdata *conn,
} }
/* called from multi.c while DOing */ /* called from multi.c while DOing */
static CURLcode pop3_doing(struct connectdata *conn, static CURLcode pop3_doing(struct connectdata *conn, bool *dophase_done)
bool *dophase_done)
{ {
CURLcode result; CURLcode result;
result = pop3_multi_statemach(conn, dophase_done); result = pop3_multi_statemach(conn, dophase_done);
@ -899,7 +890,6 @@ static CURLcode pop3_doing(struct connectdata *conn,
* *
* Performs all commands done before a regular transfer between a local and a * Performs all commands done before a regular transfer between a local and a
* remote host. * remote host.
*
*/ */
static CURLcode pop3_regular_transfer(struct connectdata *conn, static CURLcode pop3_regular_transfer(struct connectdata *conn,
bool *dophase_done) bool *dophase_done)
@ -951,11 +941,9 @@ static CURLcode pop3_setup_connection(struct connectdata * conn)
#endif #endif
} }
/* /* We explicitly mark this connection as persistent here as we're doing
* We explicitly mark this connection as persistent here as we're doing POP3 over HTTP and thus we accidentally avoid setting this value
* POP3 over HTTP and thus we accidentally avoid setting this value otherwise. */
* otherwise.
*/
conn->bits.close = FALSE; conn->bits.close = FALSE;
#else #else
failf(data, "POP3 over http proxy requires HTTP support built-in!"); failf(data, "POP3 over http proxy requires HTTP support built-in!");
@ -968,10 +956,6 @@ static CURLcode pop3_setup_connection(struct connectdata * conn)
return CURLE_OK; return CURLE_OK;
} }
/* this is the 5-bytes End-Of-Body marker for POP3 */
#define POP3_EOB "\x0d\x0a\x2e\x0d\x0a"
#define POP3_EOB_LEN 5
/* /*
* This function scans the body after the end-of-body and writes everything * This function scans the body after the end-of-body and writes everything
* until the end is found. * until the end is found.

View File

@ -26,15 +26,15 @@
* POP3 unique setup * POP3 unique setup
***************************************************************************/ ***************************************************************************/
typedef enum { typedef enum {
POP3_STOP, /* do nothing state, stops the state machine */ POP3_STOP, /* do nothing state, stops the state machine */
POP3_SERVERGREET, /* waiting for the initial greeting immediately after POP3_SERVERGREET, /* waiting for the initial greeting immediately after
a connect */ a connect */
POP3_USER, POP3_USER,
POP3_PASS, POP3_PASS,
POP3_STARTTLS, POP3_STARTTLS,
POP3_COMMAND, POP3_COMMAND,
POP3_QUIT, POP3_QUIT,
POP3_LAST /* never used */ POP3_LAST /* never used */
} pop3state; } pop3state;
/* pop3_conn is used for struct connection-oriented data in the connectdata /* pop3_conn is used for struct connection-oriented data in the connectdata
@ -52,12 +52,12 @@ struct pop3_conn {
extern const struct Curl_handler Curl_handler_pop3; extern const struct Curl_handler Curl_handler_pop3;
extern const struct Curl_handler Curl_handler_pop3s; extern const struct Curl_handler Curl_handler_pop3s;
/* /* This is the 5-bytes End-Of-Body marker for POP3 */
* This function scans the body after the end-of-body and writes everything #define POP3_EOB "\x0d\x0a\x2e\x0d\x0a"
* until the end is found. #define POP3_EOB_LEN 5
*/
CURLcode Curl_pop3_write(struct connectdata *conn, /* This function scans the body after the end-of-body and writes everything
char *str, * until the end is found */
size_t nread); CURLcode Curl_pop3_write(struct connectdata *conn, char *str, size_t nread);
#endif /* HEADER_CURL_POP3_H */ #endif /* HEADER_CURL_POP3_H */