📄 ssl_mboxdemo.c
字号:
DispEmail();
LogAddEmailEntry("sent", email.from, email.subject);
return 0;
}
/*
* Delete an email from the email list
*/
int DeleteEmail(void)
{
int prev;
int i;
if ((emailList.ptr < 0) || (emailList.ptr >= MAX_EMAILS)) {
// Index out of range
return -1;
}
emailIndex[emailList.ptr].used = 0;
if ((emailList.ptr == emailList.head) && (emailList.ptr == emailList.tail)) {
// Removing only remaining entry
emailList.head = -1;
emailList.tail = -1;
emailList.ptr = -1;
} else if (emailList.ptr == emailList.head) {
// Removing first entry
emailList.head = emailIndex[emailList.ptr].next;
emailIndex[emailList.head].prev = -1;
emailList.ptr = emailList.head;
} else if (emailList.ptr == emailList.tail) {
// Removing last entry
emailList.tail = emailIndex[emailList.ptr].prev;
emailIndex[emailList.tail].next = -1;
emailList.ptr = emailList.tail;
} else {
// Removing middle entry
emailIndex[emailIndex[emailList.ptr].prev].next = emailIndex[emailList.ptr].next;
emailIndex[emailIndex[emailList.ptr].next].prev = emailIndex[emailList.ptr].prev;
emailList.ptr = emailIndex[emailList.ptr].next;
}
FlagCheck();
return 0;
}
/*
* Parse the value of the variable, and store it in the appropriate
* FORMSpec structure. Return -1 on error.
*/
char *parsePtr;
int ParseToken(HttpState* state, int *bytesRead)
{
int i;
int retval;
int len;
for (i = 0; i < (sizeof(FORMSpec)/sizeof(FORMType)); i++) {
if (!strcmp(FORMSpec[i].name, state->buffer)) {
parsePtr = FORMSpec[i].value;
len = 0;
retval = http_sock_fastread(state, parsePtr, 1);
while ((*bytesRead < (state->content_length - 2)) &&
(http_sock_tick(state) != 0) &&
(*parsePtr != '&')) {
if (retval != 0) {
(*bytesRead)++;
if (len < (FORMSpec[i].len - 1)) {
parsePtr++;
len++;
}
}
retval = http_sock_fastread(state, parsePtr, 1);
}
*parsePtr = '\0';
}
if (i < (sizeof(FORMSpec)/sizeof(FORMType) - 1) && (http_sock_tick(state) == 0)) {
return -1;
}
}
return 1;
}
/*
* Parse the url-encoded POST data into the FORMSpec struct
* (ie: parse 'foo=bar&baz=qux' into the struct). Return -1
* on error.
*/
int ParsePost(HttpState* state)
{
int retval;
int bytesRead;
bytesRead = 0;
while ((bytesRead < (state->content_length - 2)) && (http_sock_tick(state) != 0)) {
retval = http_sock_fastread(state, state->p, 1);
if (retval != 0) {
bytesRead++;
if (*state->p == '=') {
*state->p = '\0';
state->p = state->buffer;
if (ParseToken(state, &bytesRead) == -1) {
return -1;
}
} else {
state->p++;
}
}
}
if (bytesRead == (state->content_length - 2)) {
return 1;
} else {
return -1;
}
}
/*
* Accept the new email and display it to the LCD.
*/
int Submit(HttpState* state)
{
if (state->length) {
/* buffer to write out */
if (state->offset < state->length) {
state->offset += http_sock_fastwrite(state,
state->buffer + (int)state->offset,
(int)state->length - (int)state->offset);
} else {
state->offset = 0;
state->length = 0;
}
} else {
switch (state->substate) {
case 0:
/* init the FORMSpec data */
FORMSpec[0].value[0] = '\0';
FORMSpec[1].value[0] = '\0';
FORMSpec[2].value[0] = '\0';
state->p = state->buffer;
state->substate++;
break;
case 1:
/* parse the POST information */
if (ParsePost(state) != -1) {
// Add the email to the email list
if (AddEmail(emailTemp.from, emailTemp.subject, emailTemp.body) != -1) {
state->substate++;
} else {
// Failed to add the email
state->substate = 10;
}
} else {
// Failed to parse the new email
state->substate = 10;
}
break;
case 2:
state->substate = 0;
cgi_redirectto(state,REDIRECTTO);
break;
// This case only occurs when there has been an error
case 10:
strcpy(state->buffer, "HTTP/1.0 200 OK\r\n\r\n");
state->length = strlen(state->buffer);
state->offset = 0;
state->substate++;
break;
case 11:
strcpy(state->buffer, "<HTML><HEAD><TITLE>Email Failed!</TITLE></HEAD><BODY><H1>Email failed!</H1>\r\n");
state->length = strlen(state->buffer);
state->substate++;
break;
case 12:
strcpy(state->buffer, "<P><A HREF=\"/\">Back to main page</A></BODY></HTML>\r\n");
state->length = strlen(state->buffer);
state->substate++;
break;
default:
state->substate = 0;
return 1;
}
}
return 0;
}
/*
* Process a read email request from the web browser--all emails
* are displayed.
*/
int emailCurr;
int emailNum;
int bodyPosition;
int Read(HttpState* state)
{
int i;
char line[10];
if (state->length) {
/* buffer to write out */
if (state->offset < state->length) {
state->offset += http_sock_fastwrite(state,
state->buffer + (int)state->offset,
(int)state->length - (int)state->offset);
} else {
state->offset = 0;
state->length = 0;
}
} else {
switch (state->substate) {
case 0:
strcpy(state->buffer, "HTTP/1.0 200 OK\r\n\r\n");
state->length = strlen(state->buffer);
state->offset = 0;
state->substate++;
break;
case 1:
strcpy(state->buffer, "<html><head><title>Read Email</title></head><body>\r\n");
state->length = strlen(state->buffer);
state->substate++;
break;
case 2:
emailCurr = emailList.head;
emailNum = 1;
strcpy(state->buffer, "<H1>Read Email</H1>\r\n");
state->length = strlen(state->buffer);
state->substate++;
break;
case 3:
bodyPosition = 0;
if (emailCurr != -1) {
strcpy(state->buffer, "<B>Email #");
itoa(emailNum, line);
strcat(state->buffer, line);
strcat(state->buffer, "</B><P><PRE>");
state->length = strlen(state->buffer);
state->substate++;
} else if (emailNum == 1) {
strcpy(state->buffer, "No email");
state->length = strlen(state->buffer);
state->substate = 9;
} else {
state->substate = 9;
}
break;
case 4:
xmem2root(&email, emailBuffer + sizeof(Email)*emailCurr, sizeof(Email));
strcpy(state->buffer, "FROM: ");
strcat(state->buffer, email.from);
strcat(state->buffer, "\r\n");
state->length = strlen(state->buffer);
emailNum++;
emailCurr = emailIndex[emailCurr].next;
state->substate++;
break;
case 5:
strcpy(state->buffer, "SUBJECT: ");
strcat(state->buffer, email.subject);
strcat(state->buffer, "\r\n");
state->length = strlen(state->buffer);
state->substate++;
break;
case 6:
strcpy(state->buffer, "BODY\r\n");
state->length = strlen(state->buffer);
state->substate++;
break;
case 7:
strncpy(state->buffer, email.body + bodyPosition, HTTP_MAXBUFFER);
if (state->buffer[HTTP_MAXBUFFER - 1] != '\0') {
state->buffer[HTTP_MAXBUFFER - 1] = '\0';
if (strlen(state->buffer) == (HTTP_MAXBUFFER - 1)) {
// More data to get
bodyPosition += HTTP_MAXBUFFER - 1;
} else {
state->substate++;
}
} else {
state->substate++;
}
state->length = strlen(state->buffer);
break;
case 8:
strcpy(state->buffer, "\r\n</PRE>\r\n");
state->length = strlen(state->buffer);
state->substate = 3;
break;
case 9:
strcpy(state->buffer, "<p><a href=\"/\">Back to main page</a></body></html>\r\n");
state->length = strlen(state->buffer);
state->substate++;
break;
default:
state->substate = 0;
return 1;
}
}
return 0;
}
/*
* Process a view log request--the log of email actions is displayed.
*/
int logPosition;
int ViewLog(HttpState *state)
{
if (state->length) {
/* buffer to write out */
if (state->offset < state->length) {
state->offset += http_sock_fastwrite(state,
state->buffer + (int)state->offset,
(int)state->length - (int)state->offset);
} else {
state->offset = 0;
state->length = 0;
}
} else {
switch (state->substate) {
case 0:
logPosition = 0;
strcpy(state->buffer, "HTTP/1.0 200 OK\r\n\r\n");
state->length = strlen(state->buffer);
state->offset = 0;
state->substate++;
break;
case 1:
strcpy(state->buffer, "<html><head><title>Display Log</title></head><body>\r\n");
state->length = strlen(state->buffer);
state->substate++;
break;
case 2:
strcpy(state->buffer, "<H1>Display Log</H1>\r\n<PRE>\r\n");
state->length = strlen(state->buffer);
state->substate++;
break;
case 3:
xmem2root(state->buffer, logBuffer + logPosition, HTTP_MAXBUFFER);
if (state->buffer[HTTP_MAXBUFFER - 1] != '\0') {
state->buffer[HTTP_MAXBUFFER - 1] = '\0';
if (strlen(state->buffer) == (HTTP_MAXBUFFER - 1)) {
// More data to get
logPosition += HTTP_MAXBUFFER - 1;
} else {
state->substate++;
}
} else {
state->substate++;
}
state->length = strlen(state->buffer);
break;
case 4:
strcpy(state->buffer, "\r\n</PRE>\r\n");
state->length = strlen(state->buffer);
state->substate++;
break;
case 5:
strcpy(state->buffer, "<p><a href=\"/\">Back to main page</a></body></html>\r\n");
state->length = strlen(state->buffer);
state->substate++;
break;
default:
state->substate = 0;
return 1;
}
}
return 0;
}
SSPEC_RESOURCETABLE_START
SSPEC_RESOURCE_XMEMFILE("/", index_html),
SSPEC_RESOURCE_XMEMFILE("/index.html", index_html),
SSPEC_RESOURCE_XMEMFILE("/enter.html", enter_html),
SSPEC_RESOURCE_FUNCTION("/enteremail.cgi", Submit),
SSPEC_RESOURCE_FUNCTION("/read.html", Read),
SSPEC_RESOURCE_FUNCTION("/display.html", ViewLog)
SSPEC_RESOURCETABLE_END
/*
* These constants define keypad actions
*/
#define DELETE '1'
#define READ '2'
#define EMAIL_UP '3'
#define EMAIL_DOWN '4'
#define SCROLL_UP '5'
#define SCROLL_DOWN '6'
#define IGNORE '7'
void initsystem()
{
auto int status;
brdInit(); //initialize board for this demo
dispInit();
glXFontInit(&fi6x8, 6, 8, 32, 127, Font6x8);
status = TextWindowFrame(&wholewindow, &fi6x8,0, 0, LCD_XS, LCD_YS);
keyConfig ( 0, EMAIL_DOWN, 0, 0, 0, 0, 0);
keyConfig ( 1, SCROLL_UP, 0, 0, 0, 0, 0);
keyConfig ( 2, SCROLL_DOWN, 0, 0, 0, 0, 0);
keyConfig ( 3, EMAIL_UP, 0, 0, 0, 0, 0);
keyConfig ( 4, READ, 0, 0, 0, 0, 0);
keyConfig ( 5, DELETE, 0, 0, 0, 0, 0);
keyConfig ( 6, IGNORE, 0, 0, 0, 0, 0);
}
main()
{
auto unsigned wKey;
auto int i;
/* init FORM searchable names - must init ALL FORMSpec structs! */
FORMSpec[0].name = "email_from";
FORMSpec[0].value = emailTemp.from;
FORMSpec[0].len = MAX_FROM_LEN;
FORMSpec[1].name = "email_subject";
FORMSpec[1].value = emailTemp.subject;
FORMSpec[1].len = MAX_SUBJECT_LEN;
FORMSpec[2].name = "email_body";
FORMSpec[2].value = emailTemp.body;
FORMSpec[2].len = MAX_BODY_LEN;
initsystem();
sock_init();
http_init();
// Reserve the HTTPS port 443
tcp_reserveport(443);
FlagInit();
CreateEmailList();
LogInit();
DispBufferInit();
DispNoEmail();
while (1) {
http_handler();
costate
{
keyProcess(); //scans for keypress
waitfor (DelayMs(10));
}
costate
{
waitfor (wKey = keyGet()); //get key from buffer
switch (wKey)
{
case DELETE:
if (emailList.ptr != -1) {
LogAddEmailEntry("deleted", email.from, email.subject);
DeleteEmail();
}
DispEmail();
break;
case READ:
if ((emailList.ptr != -1) && (emailIndex[emailList.ptr].read != 1)) {
LogAddEmailEntry("read", email.from, email.subject);
emailIndex[emailList.ptr].read = 1;
FlagCheck();
}
break;
case EMAIL_UP:
if (emailList.ptr != -1) {
if (emailIndex[emailList.ptr].prev != -1) {
emailList.ptr = emailIndex[emailList.ptr].prev;
}
DispEmail();
}
break;
case EMAIL_DOWN:
if (emailList.ptr != -1) {
if (emailIndex[emailList.ptr].next != -1) {
emailList.ptr = emailIndex[emailList.ptr].next;
}
DispEmail();
}
break;
case SCROLL_UP:
if ((emailList.ptr != -1) && (emailDispLine > 0)) {
emailDispLine--;
DispUpdateWindow();
}
break;
case SCROLL_DOWN:
if ((emailList.ptr != -1) && ((emailDispLine + 4) < emailLastLine)) {
emailDispLine++;
DispUpdateWindow();
}
break;
default: // Do nothing
break;
} //end switch
} //end costate
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -