[BACK]Return to sm1Socket.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / kan96xx / plugin

Annotation of OpenXM/src/kan96xx/plugin/sm1Socket.c, Revision 1.12

1.12    ! takayama    1: /* $OpenXM: OpenXM/src/kan96xx/plugin/sm1Socket.c,v 1.11 2002/10/24 01:05:05 takayama Exp $ */
1.1       maekawa     2: /* msg0s.c */
                      3: #include <stdio.h>
                      4: #include <sys/types.h>
                      5: #include <sys/socket.h>
                      6: #include <sys/time.h>
                      7: #include <netinet/in.h>
                      8: #include <netdb.h>
                      9:
                     10:
                     11: #include "datatype.h"
                     12: #include "stackm.h"
                     13: #include "extern.h"
                     14: #include "sm1Socket.h"
                     15:
                     16: extern int Quiet;
                     17: static void errorMsg1s(char *s);
1.8       takayama   18: static int getContentLength(char *s);
                     19: static int getReceivedContentLength(char *s);
1.6       takayama   20: #define MAX_LISTEN_QUEUE 3
1.1       maekawa    21:
                     22: /* [(sm1.socket) (open) [optional integer port, optional string name] ] extension ; */
                     23: struct object KsocketOpen(struct object obj) {
                     24:   char serverName[1024];
                     25:   int portNumber;
                     26:   struct object rob = NullObject;
                     27:   struct hostent *myhost;
                     28:   struct sockaddr_in me;
                     29:   int s_waiting;
                     30:   int on;
                     31:   int tt;
                     32:   extern int errno;
                     33:
                     34:   if (obj.tag != Sarray) {
                     35:     errorMsg1s("KsocketOpen([optional integer,optional string name])");
                     36:   }
                     37:   strcpy(serverName,"localhost");
                     38:   portNumber = 0;
                     39:   if (getoaSize(obj) >= 1) {
                     40:     if ((getoa(obj,0)).tag != Sinteger) {
                     41:       errorMsg1s("KsocketOpen([optional integer,optional string name]), the first argument must be an integer.");
                     42:     }
                     43:     portNumber = KopInteger(getoa(obj,0));
                     44:   }
                     45:   if (getoaSize(obj) >= 2) {
                     46:     if ((getoa(obj,1)).tag != Sdollar) {
                     47:       errorMsg1s("KsocketOpen([optional integer,optional string name]), the second argument must be a string.");
                     48:     }
                     49:     if (strlen(KopString(getoa(obj,1))) > 1023) {
                     50:       errorMsg1s("Too long server name");
                     51:     }
                     52:     strcpy(serverName,KopString(getoa(obj,1)));
                     53:   }
                     54:
                     55:
                     56:   /* fprintf(stderr,"Hello from open.\n"); */
                     57:   if ((myhost = gethostbyname(serverName)) == NULL) {
                     58:     errorMsg1s("Bad server name.");
                     59:   }
                     60:   bzero((char *)&me,sizeof(me));
                     61:   me.sin_family = AF_INET;
                     62:   me.sin_port = htons(portNumber);
                     63:   bcopy(myhost->h_addr,
1.3       takayama   64:         &me.sin_addr,myhost->h_length);
1.1       maekawa    65:
                     66:   if ((s_waiting = socket(AF_INET,SOCK_STREAM,0)) < 0) {
                     67:     errorMsg1s("Socket allocation is failed.");
                     68:   }
                     69:
                     70:   on=1; setsockopt(s_waiting,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
                     71:   /* important */
                     72:   if (bind(s_waiting,(struct sockaddr *) &me,sizeof(me)) == -1) {
                     73:     fprintf(stderr,"Bind error. Error no is %d. See /usr/include/sys/errno.h.\n",errno);
                     74:     errorMsg1s("cannot bind");
                     75:   }
                     76:
                     77:   tt = sizeof(me);
                     78:   if (getsockname(s_waiting,(struct sockaddr *)&me,&tt) < 0) {
                     79:     fprintf(stderr,"getsockname error. Error no is %d. See /usr/include/sys/errno.h.\n",errno);
                     80:     errorMsg1s("cannot getsockname");
                     81:   }
                     82:
                     83:
1.6       takayama   84:   if (listen(s_waiting,MAX_LISTEN_QUEUE) < 0) {
1.1       maekawa    85:     errorMsg1s("Listen failed");
                     86:   }
                     87:   /*
1.3       takayama   88:       fprintf(stderr,"Done the initialization. port =%d\n",ntohs(me.sin_port));
                     89:       fprintf(stderr,"socket id = %d",accept(s_waiting,NULL,NULL)); */
1.1       maekawa    90:   rob = newObjectArray(2);
                     91:   putoa(rob,0,KpoInteger(s_waiting));
                     92:   putoa(rob,1,KpoInteger(ntohs(me.sin_port)));
                     93:   return(rob);
                     94:
                     95: }
                     96:
                     97: /* [ integer port, optional string host default localhost]
                     98:    [ integer socketid, integer port ] */
                     99: struct object KsocketConnect(struct object obj) {
                    100:   char serverName[1024];
                    101:   int portNumber;
                    102:   struct hostent *servhost;
                    103:   struct sockaddr_in server;
                    104:   int socketid;
                    105:   struct object robj = NullObject;
                    106:
                    107:   if (obj.tag != Sarray) {
                    108:     errorMsg1s("KsocketConnect( [ integer port, optional string host default localhost])");
                    109:   }
                    110:   strcpy(serverName,"localhost");
                    111:   if (getoaSize(obj) < 1) {
                    112:     errorMsg1s("You need to specify port number.\n");
                    113:   }
                    114:   if (getoaSize(obj) >= 1) {
                    115:     if ((getoa(obj,0)).tag != Sinteger) {
                    116:       errorMsg1s("KsocketConnect([integer port,optional string host]), the first argument must be an integer.");
                    117:     }
                    118:     portNumber = KopInteger(getoa(obj,0));
                    119:   }
                    120:   if (getoaSize(obj) >= 2) {
                    121:     if ((getoa(obj,1)).tag != Sdollar) {
                    122:       errorMsg1s("KsocketConnect([integer port,optional string host]), the second argument must be a string.");
                    123:     }
                    124:     if (strlen(KopString(getoa(obj,1))) > 1023) {
                    125:       errorMsg1s("Too long server name");
                    126:     }
                    127:     strcpy(serverName,KopString(getoa(obj,1)));
                    128:   }
                    129:
                    130:
                    131:   if ((servhost = gethostbyname(serverName)) == NULL) {
                    132:     errorMsg1s("bad server name.\n");
                    133:   }
                    134:   bzero((char *)&server,sizeof(server));
                    135:   server.sin_family = AF_INET;
                    136:   server.sin_port = htons(portNumber);
                    137:   bcopy(servhost->h_addr,
1.3       takayama  138:         (char *)&server.sin_addr,servhost->h_length);
1.1       maekawa   139:
                    140:   if ((socketid = socket(AF_INET,SOCK_STREAM,0)) <0) {
                    141:     errorMsg1s("socket allocation is failed.\n");
                    142:   }
                    143:   if (!Quiet) {
                    144:     fprintf(stderr,"Trying to connect port %d, ip=%x\n",ntohs(server.sin_port),server.sin_addr);
                    145:   }
                    146:   if (connect(socketid,(struct sockaddr *)&server,sizeof(server)) == -1) {
                    147:     errorMsg1s("cannot connect");
                    148:   }
                    149:   /* fprintf(stderr,"connected.\n"); */
                    150:   robj = newObjectArray(2);
                    151:   putoa(robj,0,KpoInteger(socketid));
                    152:   putoa(robj,1,KpoInteger(portNumber));
                    153:   return(robj);
                    154: }
                    155:
                    156: /* [ integer socketid ]
                    157:    [ integer newsocketid ] */
                    158: struct object KsocketAccept(struct object obj) {
                    159:   struct object obj1;
                    160:   struct object obj2;
                    161:   struct object robj;
                    162:   int s, news;
                    163:
                    164:   if (obj.tag != Sarray) {
                    165:     errorMsg1s("KsocketAccept([integer socketid])");
                    166:   }
                    167:   if (getoaSize(obj) < 1) {
                    168:     errorMsg1s("KsocketAccept([integer socketid])");
                    169:   }
                    170:   obj1 = getoa(obj,0);
                    171:   if (obj1.tag != Sinteger ) {
                    172:     errorMsg1s("KsocketAccept([integer socketid]), argument must be integer.");
                    173:   }
                    174:   s = KopInteger(obj1);
                    175:   if ((news = accept(s,NULL,NULL)) < 0) {
                    176:     errorMsg1s("Error in accept.");
                    177:   }
                    178:   if (close(s) < 0) {
                    179:     errorMsg1s("Error in closing the old socket.");
                    180:   }
                    181:   robj = newObjectArray(1);
                    182:   putoa(robj,0,KpoInteger(news));
                    183:   return(robj);
                    184: }
                    185:
                    186: int KsocketSelect0(int fd,int t) {
                    187:   fd_set readfds;
                    188:   struct timeval timeout;
                    189:   extern int errno;
                    190:   FD_ZERO(&readfds);
                    191:   FD_SET(fd,&readfds);
                    192:   timeout.tv_sec = 0;
                    193:   timeout.tv_usec = (long) t;
                    194:   if (t >= 0) {
                    195:     if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,&timeout)<0) {
                    196:       /* It must be fd+1 !,  Not fd. */
                    197:       fprintf(stderr,"Select error. Error no is %d. See /usr/include/sys/errno.h.\n",errno);
                    198:       errorMsg1s("KsocketSelect0() : select failed.");
                    199:       return(0);
                    200:     }
                    201:   }else{ /* block */
                    202:     if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,(struct timeval *)NULL)<0) {
                    203:       errorMsg1s("KsocketSelect0() : select failed.");
                    204:       fprintf(stderr,"Select error. Error no is %d. See /usr/include/sys/errno.h.\n",errno);
                    205:       return(0);
                    206:     }
                    207:   }
                    208:   if (FD_ISSET(fd,&readfds)) return(1);
                    209:   else return(0);
                    210: }
                    211:
                    212: /* [ integer socketid optional integer timeout default 0]
                    213:    integer true or false
                    214: */
                    215: struct object KsocketSelect(struct object obj) {
                    216:   struct object robj;
                    217:   struct object ob1;
                    218:   struct object ob2;
                    219:   if (obj.tag != Sarray) {
                    220:     errorMsg1s("KsocketSelect([ integer socketid optional integer timeout default 0]");
                    221:   }
                    222:   if (getoaSize(obj) < 1) {
                    223:     errorMsg1s("KsocketSelect([ integer socketid optional integer timeout default 0]");
                    224:   }
                    225:   if (getoaSize(obj) >= 1) {
                    226:     ob1 = getoa(obj,0);
                    227:     ob2 = KpoInteger(0); /* default value */
                    228:   }
                    229:   if (getoaSize(obj) >= 2) {
                    230:     ob2 = getoa(obj,1);
                    231:   }
                    232:   if (ob1.tag != Sinteger) {
                    233:     errorMsg1s("KsocketSelect([ integer socketid optional integer timeout default 0] : the first argument must be an integer.");
                    234:   }
                    235:   if (ob2.tag != Sinteger) {
                    236:     errorMsg1s("KsocketSelect([ integer socketid optional integer timeout default 0] : the second argument must be an integer.");
                    237:   }
                    238:   robj = KpoInteger( KsocketSelect0(KopInteger(ob1),KopInteger(ob2)) );
                    239:   return(robj);
                    240: }
                    241:
                    242: struct object KsocketSelectMulti(struct object obj)
1.3       takayama  243:      /* [ [integer socketid1, integer socketid2, ...]
1.1       maekawa   244:      optional integer timeout default 0]
                    245:    [ result1, result2, ....]
                    246: */
                    247: {
                    248:   struct object robj;
                    249:   struct object ob1;
                    250:   struct object ob2;
                    251:   struct object ob3;
                    252:   int size,i,fd,p,t;
                    253:   fd_set readfds;
                    254:   struct timeval timeout;
                    255:   extern errno;
                    256:   if (obj.tag != Sarray) {
                    257:     errorMsg1s("KsocketSelectMulti([[sid1, sid2,...] optional integer timeout default 0]");
                    258:   }
                    259:   if (getoaSize(obj) < 1) {
                    260:     errorMsg1s("KsocketSelectMulti([[sid1, sid2,...] optional integer timeout default 0]");
                    261:   }
                    262:   if (getoaSize(obj) >= 1) {
                    263:     ob1 = getoa(obj,0);
                    264:     ob2 = KpoInteger(0); /* default value */
                    265:   }
                    266:   if (getoaSize(obj) >= 2) {
                    267:     ob2 = getoa(obj,1);
                    268:   }
                    269:   if (ob1.tag != Sarray) {
                    270:     errorMsg1s("KsocketSelectMulti([[sid1, sid2, ...] optional integer timeout default 0] : the first argument must be an array.");
                    271:   }
                    272:   if (ob2.tag != Sinteger) {
                    273:     errorMsg1s("KsocketSelectMulti([[sid1, sid2, ...] optional integer timeout default 0] : the second argument must be an integer.");
                    274:   }
                    275:   FD_ZERO(&readfds);
                    276:   timeout.tv_sec = 0; t =  KopInteger(ob2);
                    277:   timeout.tv_usec = (long)t;
                    278:
                    279:   size = getoaSize(ob1);
                    280:   if (size < 1) {
                    281:     errorMsg1s("KsocketSelectMulti: the first argument must be a non-empty array of integers.");
                    282:   }
                    283:   fd = 0;
                    284:
                    285:   for (i=0; i<size; i++) {
                    286:     ob3 = getoa(ob1,i);
                    287:     if (ob3.tag != Sinteger) {
                    288:       errorMsg1s("KsocketSelectMulti: the first argument must be an array of integers.");
                    289:     }
                    290:     p = KopInteger(ob3);
                    291:     if (p > fd) fd = p;
                    292:     FD_SET(p,&readfds);
                    293:     /* printf("p = %d, fd=%d",p,fd); */
                    294:   }
                    295:
                    296:   if (t >= 0) {
                    297:     if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,&timeout)<0) {
                    298:       /* It must be fd+1 !,  Not fd. */
                    299:       fprintf(stderr,"Select error. Error no is %d. See /usr/include/sys/errno.h.\n",errno);
                    300:       errorMsg1s("KsocketSelectMulti() : select failed.");
                    301:     }
                    302:   }else{ /* block */
                    303:     if (select(fd+1,&readfds,(fd_set *)NULL,(fd_set *)NULL,(struct timeval *)NULL)<0) {
                    304:       fprintf(stderr,"Select error. Error no is %d. See /usr/include/sys/errno.h.\n",errno);
                    305:       errorMsg1s("KsocketSelectMulti() : (block) select failed.");
                    306:     }
                    307:   }
                    308:   robj = newObjectArray(size);
                    309:   for (i=0; i<size; i++) {
                    310:     if (FD_ISSET(KopInteger(getoa(ob1,i)),&readfds)) {
                    311:       putoa(robj,i,KpoInteger(1));
                    312:     }else{
                    313:       putoa(robj,i,KpoInteger(0));
                    314:     }
                    315:   }
                    316:
                    317:   return(robj);
                    318: }
                    319:
                    320:
                    321:
                    322:
                    323: static char Data00[1024];
                    324: /* [ integer socketid ]
                    325:    string data
                    326: */
                    327: struct object KsocketRead(struct object obj) {
                    328:   struct object ob1;
                    329:   struct object robj = NullObject;
                    330:   static int datasize = 1024;
                    331:   static char *data = Data00;
                    332:   char *tmp;
                    333:   char *r;
                    334:   int n;
                    335:   int socketid;
                    336:   int totalsize;
                    337:
                    338:   if (obj.tag != Sarray) {
                    339:     errorMsg1s("KsocketRead([integer socketid])");
                    340:   }
                    341:   if (getoaSize(obj) < 1) {
                    342:     errorMsg1s("KsocketRead([integer socketid])");
                    343:   }
                    344:   ob1 = getoa(obj,0);
                    345:   if (ob1.tag != Sinteger) {
                    346:     errorMsg1s("KsocketRead([integer socketid]) : the argument must be integer.");
                    347:   }
                    348:   socketid = KopInteger(ob1);
                    349:
                    350:   totalsize = 0;
                    351:
                    352:   while (KsocketSelect0(socketid,0)) {
                    353:     if (datasize - totalsize > 0) {
                    354:       n = read(socketid,data+totalsize,datasize-totalsize);
                    355:       if (n < 0) {
1.3       takayama  356:         errorMsg1s("Read error.");
1.1       maekawa   357:       }
                    358:       if (n < datasize-totalsize) {
1.3       takayama  359:         totalsize += n;
                    360:         break;
1.1       maekawa   361:       }else{ totalsize += n; }
                    362:       if (totalsize == 0) {
1.3       takayama  363:         errorMsg1s("Select returns 1, but there is no data to read.");
1.1       maekawa   364:       }
                    365:     }else { /* increase the datasize */
                    366:       tmp = (char *)GC_malloc(sizeof(char)*2*datasize);
                    367:       /*I should use GC_malloc_atomic and free after finishing this function?*/
                    368:       if (tmp == (char *)NULL) errorMsg1s("Out of Memory.");
                    369:       bcopy(data,tmp,totalsize);
                    370:       data = tmp;
                    371:       datasize = 2*datasize;
                    372:     }
                    373:   }
                    374:
1.5       takayama  375:   r = (char *)GC_malloc(sizeof(char)*(totalsize+1));
1.1       maekawa   376:   if (r == (char *)NULL) errorMsg1s("Out of Memory.");
                    377:   bcopy(data,r,totalsize);
1.5       takayama  378:   r[totalsize] = 0;
                    379:   robj = KpoString(r);  /* BUG: it works only for reading string from TCP/IP
                    380:                                                    stream. */
1.1       maekawa   381:
                    382:   return(robj);
                    383:
                    384: }
                    385:
                    386: /* [ integer socketid, string data ]
                    387:    integer  */
                    388: struct object KsocketWrite(struct object obj) {
                    389:   struct object ob1;
                    390:   struct object ob2;
                    391:   int socketid;
                    392:   int r;
                    393:   if (obj.tag != Sarray) {
                    394:     errorMsg1s("KsocketWrite([integer socketid, string data])");
                    395:   }
                    396:   if (getoaSize(obj) < 2) {
                    397:     errorMsg1s("KsocketWrite([integer socketid, string data])");
                    398:   }
                    399:   ob1 = getoa(obj,0);
                    400:   ob2 = getoa(obj,1);
                    401:   if (ob1.tag != Sinteger) {
                    402:     errorMsg1s("KsocketWrite([integer socketid, string data]) : the first argument must be an integer.");
                    403:   }
                    404:   socketid = KopInteger(ob1);
                    405:   if (ob2.tag != Sdollar) {
                    406:     errorMsg1s("KsocketWrite([integer socketid, string data]) : the second argument must be a string.");
                    407:   }
                    408:   r = write(socketid,KopString(ob2), strlen(KopString(ob2)));
                    409:   return(KpoInteger(r));
                    410:
                    411: }
                    412: struct object KsocketClose(struct object obj) {
                    413:   int socketid;
                    414:   struct object ob1;
                    415:   if (obj.tag != Sarray) {
                    416:     errorMsg1s("KsocketClose([ integer socketid ])");
                    417:   }
                    418:   if (getoaSize(obj) != 1) {
                    419:     errorMsg1s("KsocketClose([ integer socketid ])");
                    420:   }
                    421:   ob1 = getoa(obj,0);
                    422:   if (ob1.tag != Sinteger) {
                    423:     errorMsg1s("KsocketClose([ INTEGER socketid ])");
                    424:   }
                    425:   socketid = KopInteger(ob1);
                    426:   return(KpoInteger(close(socketid)));
                    427: }
                    428:
                    429:
                    430: static void errorMsg1s(char *s) {
                    431:   fprintf(stderr,"%s\n",s);
                    432:   errorKan1("msg1s.c: %s\n",s);
                    433: }
                    434:
                    435:
                    436: /**************** new 1997, 11/23 *******************/
                    437: struct object KsocketReadByte(struct object obj);
                    438: struct object KsocketWriteByte(struct object obj);
                    439:
                    440: struct object KsocketReadByte(struct object obj) {
                    441:   struct object ob1;
                    442:   struct object robj = NullObject;
                    443:   char data[2];
                    444:   char *tmp;
                    445:   char *r;
                    446:   int n;
                    447:   int socketid;
                    448:
                    449:
                    450:   if (obj.tag != Sarray) {
                    451:     errorMsg1s("KsocketReadByte([integer socketid])");
                    452:   }
                    453:   if (getoaSize(obj) < 1) {
                    454:     errorMsg1s("KsocketReadByte([integer socketid])");
                    455:   }
                    456:   ob1 = getoa(obj,0);
                    457:   if (ob1.tag != Sinteger) {
                    458:     errorMsg1s("KsocketReadByte([integer socketid]) : the argument must be integer.");
                    459:   }
                    460:   socketid = KopInteger(ob1);
                    461:
                    462:
                    463:   n = read(socketid,data,1);
                    464:   if (n < 0) {
                    465:     errorMsg1s("Read error.");
                    466:     robj = KpoInteger(-1);
                    467:     return(robj);
                    468:   }
                    469:   if (n == 0) {
                    470:     errorMsg1s("Read returned without data.");
                    471:   }
                    472:
                    473:   robj = KpoInteger((int)((unsigned char)data[0]));
                    474:   return(robj);
                    475:
                    476: }
                    477:
                    478: /* [ integer socketid, int ]
                    479:    integer  */
                    480: struct object KsocketWriteByte(struct object obj) {
                    481:   struct object ob1;
                    482:   struct object ob2;
                    483:   int socketid;
1.4       takayama  484:   int r,i,n,kk,r0;
                    485: #define DATA_SIZE 1024
                    486:   char data[DATA_SIZE];
1.1       maekawa   487:   if (obj.tag != Sarray) {
1.4       takayama  488:     errorMsg1s("KsocketWriteByte([integer socketid, int | array of int])");
1.1       maekawa   489:   }
                    490:   if (getoaSize(obj) < 2) {
1.4       takayama  491:     errorMsg1s("KsocketWriteByte([integer socketid, int | array of int])");
1.1       maekawa   492:   }
                    493:   ob1 = getoa(obj,0);
                    494:   ob2 = getoa(obj,1);
                    495:   if (ob1.tag != Sinteger) {
1.4       takayama  496:     errorMsg1s("KsocketWriteByte([integer socketid, int | array of int]) : the first argument must be an integer.");
1.1       maekawa   497:   }
                    498:   socketid = KopInteger(ob1);
1.4       takayama  499:   if (ob2.tag != Sinteger && ob2.tag != Sarray) {
                    500:     errorMsg1s("KsocketWriteByte([integer socketid, int | array of int]) : the second argument must be an integer or an array of integers.");
                    501:   }
                    502:   if (ob2.tag == Sinteger) {
                    503:     data[0] = KopInteger(ob2);
                    504:     r = write(socketid,data, 1);
                    505:   }else{
                    506:     n = getoaSize(ob2); kk = 0; r = 0;
                    507:     for (i=0; i<n; i++) {
                    508:       if (getoa(ob2,i).tag != Sinteger)
                    509:            errorMsg1s("KsocketWriteByte([integer socketid, int | array of int]) : elements of the second argument must be integers.");
                    510:       data[kk] = KopInteger(getoa(ob2,i));
                    511:       kk++;
                    512:       if (kk >= DATA_SIZE) {
                    513:        r0 = write(socketid,data,kk);
                    514:        if (r0 != kk) {
                    515:          fprintf(stderr,"Warning: Could not write to the socket.\n");
                    516:          return(KpoInteger(r+r0));
                    517:        }
                    518:         r += r0;
                    519:        kk = 0;
                    520:       }
                    521:     }
                    522:     if (kk > 0) {
                    523:       r0 = write(socketid,data,kk);
                    524:       if (r0 != kk) {
                    525:        fprintf(stderr,"Warning: Could not write to the socket.\n");
                    526:        return(KpoInteger(r+r0));
                    527:       }
                    528:       r += r0;
                    529:     }
1.1       maekawa   530:   }
                    531:   return(KpoInteger(r));
                    532: }
                    533:
1.7       takayama  534: struct object KsocketReadHTTP(struct object socketObj) {
                    535:   /* Read until two empty line appears. */
1.5       takayama  536:   struct object ob;
                    537:   char *s;
                    538:   char *sss;
                    539:   char *tmp;
                    540:   int i;
                    541:   int flag;
1.7       takayama  542:   int flagmax = 1;
1.5       takayama  543:   int datasize;
1.7       takayama  544:   int last;
1.8       takayama  545:   int contentLength=-1;
1.5       takayama  546:   ob = KsocketRead(socketObj);
                    547:   s = KopString(ob);
1.8       takayama  548:   if (strncmp(s,"POST",4) == 0) flagmax=2; /* for IE */
1.7       takayama  549:   else flagmax=1;
1.5       takayama  550:   flag = 0;
                    551:   for (i=strlen(s)-1; i>=0; i--) {
                    552:        if ((s[i] == '\n') && (i==0)) {
1.7       takayama  553:          ++flag;
1.5       takayama  554:        }else if ((s[i] == '\n') && (s[i-1] == '\n')) {
1.7       takayama  555:          ++flag;
1.5       takayama  556:        }else if ((s[i] == 0xd) && (s[i+1] == 0xa) && (i == 0)) {
1.7       takayama  557:          ++flag;
1.5       takayama  558:        }else if ((s[i] == 0xa) && (s[i-1] == 0xd) && (s[i+1] == 0xd) && (s[i+2] == 0xa)) {
1.7       takayama  559:          ++flag;
1.5       takayama  560:        }
                    561:   }
1.7       takayama  562:   if (flag >= flagmax) return ob;
1.5       takayama  563:   datasize = strlen(s);
                    564:   sss = s;
1.7       takayama  565:   if ((s[strlen(s)-1] == '\n') ||
                    566:       (s[strlen(s)-2] == 0xd) && (s[strlen(s)-1] == 0xa)) {
                    567:        last = 1;
                    568:   }else last = 0;
1.5       takayama  569:
1.7       takayama  570:   while (flag < flagmax) {
1.8       takayama  571:        contentLength = getContentLength(sss);
                    572:        if (contentLength != -1) {
                    573:          if (contentLength <= getReceivedContentLength(sss)) {
                    574:                break;
                    575:          }
                    576:        }
1.10      takayama  577:        fprintf(stderr,"Waiting in socketReadBlock (spin lock to wait an empty line). flagmax(0d,0a)=%d, content-length=%d, received content-length=%d\n",flagmax,contentLength,getReceivedContentLength(sss));
1.5       takayama  578:        if (strlen(s) == 0) {fprintf(stderr,"but I'm not receiving data. Expecting a bug.\n");
                    579:        }else{
                    580:          /* for debugging. */
                    581:          for (i=0; i<strlen(sss); i++) {
1.8       takayama  582:                if ((sss[i] >= ' ') && (sss[i] < 0x7f)) {
                    583:                  fprintf(stderr,"%c",sss[i]);
                    584:                }else{
1.9       takayama  585:           fprintf(stderr,"(%3x)",sss[i]);
1.8       takayama  586:                  if (sss[i] == 0xa) fprintf(stderr,"\n");
                    587:                }
                    588:
1.5       takayama  589:          }
                    590:          fprintf(stderr,"\n");
                    591:        }
                    592:        sleep(2);
                    593:        ob = KsocketRead(socketObj);
                    594:        s = KopString(ob);
                    595:        for (i=strlen(s)-1; i>=0; i--) {
1.7       takayama  596:          if ((s[i] == '\n') && (i==0) && last) {
                    597:                ++flag;
1.5       takayama  598:          }else if ((s[i] == '\n') && (s[i-1] == '\n')) {
1.7       takayama  599:                ++flag;
                    600:          }else if ((s[i] == 0xd) && (s[i+1] == 0xa) && (i==0) && last) {
                    601:                ++flag;
1.5       takayama  602:          }else if ((s[i] == 0xa) && (s[i-1] == 0xd) && (s[i+1] == 0xd) && (s[i+2] == 0xa)) {
1.7       takayama  603:                ++flag;
1.5       takayama  604:          }
                    605:        }
                    606:        if (datasize-1 <= strlen(sss)+strlen(s)) {
                    607:       tmp = (char *)GC_malloc(sizeof(char)*2*(datasize+strlen(s))+1);
                    608:       if (tmp == (char *)NULL) errorMsg1s("Out of Memory.");
                    609:          strcpy(tmp,sss);
                    610:          strcat(tmp,s);
                    611:       datasize = 2*(datasize+strlen(s));
                    612:          sss = tmp;
                    613:        }else{
                    614:          strcat(sss,s);
                    615:        }
1.7       takayama  616:
                    617:        if ((s[strlen(s)-1] == '\n') ||
                    618:                (s[strlen(s)-2] == 0xd) && (s[strlen(s)-1] == 0xa)) {
                    619:          last = 1;
                    620:        }else last = 0;
                    621:
1.5       takayama  622:   }
                    623:
                    624:   return KpoString(sss);
                    625:
                    626: }
                    627:
1.1       maekawa   628: struct object Kplugin_sm1Socket(char *key,struct object obj) {
                    629:   struct object robj = NullObject;
                    630:   if (strcmp(key,"open") == 0) {
                    631:     robj = KsocketOpen(obj);
                    632:   }else if (strcmp(key,"connect") == 0) {
                    633:     robj = KsocketConnect(obj);
                    634:   }else if (strcmp(key,"accept") == 0) {
                    635:     robj = KsocketAccept(obj);
                    636:   }else if (strcmp(key,"select") == 0) {
                    637:     robj = KsocketSelect(obj);
                    638:   }else if (strcmp(key,"mselect") == 0) {
                    639:     robj = KsocketSelectMulti(obj);
                    640:   }else if (strcmp(key,"read") == 0) {
                    641:     robj = KsocketRead(obj);
1.7       takayama  642:   }else if (strcmp(key,"readHTTP") == 0) {
                    643:     robj = KsocketReadHTTP(obj);
1.11      takayama  644:   }else if (strcmp(key,"gethostname") == 0) {
                    645:     robj = KsocketGetHostName();
                    646:   }else if (strcmp(key,"write") == 0) {
1.1       maekawa   647:     robj = KsocketWrite(obj);
                    648:   }else if (strcmp(key,"read") == 0) {
                    649:     robj = KsocketRead(obj);
                    650:   }else if (strcmp(key,"readByte") == 0) {
                    651:     robj = KsocketReadByte(obj);
                    652:   }else if (strcmp(key,"writeByte") == 0) {
                    653:     robj = KsocketWriteByte(obj);
                    654:   }else if (strcmp(key,"close") == 0) {
                    655:     robj = KsocketClose(obj);
                    656:   }else {
                    657:     errorMsg1s("Unknown tag for sm1.socket");
                    658:   }
                    659:   return(robj);
                    660: }
                    661:
                    662:
1.8       takayama  663: static int getContentLength(char *s) {
                    664:   int n;
1.10      takayama  665:   int i,j;
1.8       takayama  666:   int len = -1;
1.10      takayama  667:   char *s1 = "content-length:";
                    668:   char s0[256];
                    669:   int m;
                    670:   m = strlen(s1);
1.8       takayama  671:   n = strlen(s);
                    672:   for (i=0; i<n; i++) {
1.10      takayama  673:     strncpy(s0,&(s[i]),m+1);
                    674:     for (j=0; j<m; j++) {
                    675:       if ((s0[j] >= 'A') && (s0[j] <= 'Z')) s0[j] = s0[j]+0x20;
                    676:     }
                    677:     if (strncmp(s0,s1,strlen(s1)) == 0) {
                    678:       sscanf(&(s[i+strlen(s1)]),"%d",&len);
                    679:       break;
                    680:     }
1.8       takayama  681:   }
                    682:   return len;
                    683: }
                    684: static int getReceivedContentLength(char *s) {
                    685:   int n;
                    686:   int i;
                    687:   int start;
                    688:   start = -1;
                    689:   n = strlen(s);
                    690:   for (i=0; i<n; i++) {
                    691:        if ((s[i] == '\n') && (s[i+1] == '\n')) {
                    692:          start = i+2; break;
                    693:        }else if ((s[i] == 0xd) && (s[i+1] == 0xa) && (s[i+2] == 0xd) && (s[i+3] == 0xa)) {
                    694:          start = i+4;
                    695:        }
                    696:   }
                    697:   if (start == -1) return 0;
                    698:   return (n-start);
                    699: }
1.1       maekawa   700:
                    701:
1.11      takayama  702: struct object KsocketGetHostName(void) {
                    703:   char name[1024];
                    704:   char *s;
                    705:   struct object rob = NullObject;
                    706:   if (gethostname(name,1023) != 0) {
                    707:     return rob;
                    708:   }
                    709:   s = (char *)GC_malloc(sizeof(char)*(strlen(name)+2));
                    710:   if (s == (char *)NULL) errorMsg1s("Out of Memory.");
                    711:   strcpy(s,name);
                    712:   return(KpoString(s));
                    713: }
1.1       maekawa   714:
                    715:
                    716:
                    717:
                    718:
                    719:

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>